yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

beforeRun on \yii\base\InlineAction #18467

Open djidji01 opened 3 years ago

djidji01 commented 3 years ago

What steps will reproduce the problem?

Wouldn't it be great to wrap this https://github.com/yiisoft/yii2/blob/ff0760142d768b03e192098d94a3c0edc2710990/framework/base/InlineAction.php#L57

in a beforeRun condition as it is here https://github.com/yiisoft/yii2/blob/ff0760142d768b03e192098d94a3c0edc2710990/framework/base/Action.php#L93 on a standalone action?

What is the expected result?

This may help doing all logic and rules beforeRun and/or beforeAction while having all data ready, especially all arguments to be bound to the action that at this level are accessible on Yii::$app->requestedParams

What do you get instead?

BeforeRun is not implemented on \yii\base\inlineAction and Yii::$app->requestedParams is not yet initialized ??!!!

Additional info

I would suggest to have a beforeActionRun($action) on controller may be implemented as bellow

        if ($this->controller->beforeActionRun($action)) {
            $result = call_user_func_array([$this, 'run'], $args);
            $this->controller->afterActionRun($action);

            return $result;
        }

or just move the Controller::beforeAction($action) and Module::beforeAction($action) around this call_user_func_array([$this, 'run'], $args); which will help accessing Yii::$app->requestedParams`

Thank you very much

Q A
Yii version 2.0.?
PHP version
Operating system

bizley commented 3 years ago

But Yii::$app->requestedParams are initialized here. Could you provide use case for having additional beforeRun in the InlineAction?

djidji01 commented 3 years ago

But Yii::$app->requestedParams are initialized here. Could you provide use case for having additional beforeRun in the InlineAction?

In fact the use case should be the same as beforeRun implemented in standalone Action. Any way , why was beforeRun implemented on former and not on the later, yet both are to controlle the execution of controller method!?

Just providing possibility of additional logic while having Yii::$app->requestedParams before execution as it is for standalone Action. especially when arguments are action injected!!! as example, in bellow code, I may need to run the actionBook() only if the context meets a condition defined by $bookingService property or method or vice versa!!

Thanks

namespace app\controllers;

use yii\web\Controller;
use app\components\BookingInterface;

class HotelController extends Controller
{    
    public function actionBook($id, BookingInterface $bookingService)
    {
        $result = $bookingService->book($id);
        // ...    
    }
}
djidji01 commented 3 years ago

But Yii::$app->requestedParams are initialized here. Could you provide use case for having additional beforeRun in the InlineAction?

although it is initialized there, it is where I do see the limitation since it is done after this beforeAction() call. Also the condition above it will almost always evaluate to false unless this is null, which is not possible.

Thanks