Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beforeRun() and afterRun() do not get executed in base\Action #2549

Closed
ivokund opened this issue Feb 26, 2014 · 5 comments
Closed

beforeRun() and afterRun() do not get executed in base\Action #2549

ivokund opened this issue Feb 26, 2014 · 5 comments
Assignees
Labels
Milestone

Comments

@ivokund
Copy link
Contributor

ivokund commented Feb 26, 2014

Hi,
I see that beforeRun and afterRun were added in yii\base\Action in @a5968a6a, but I don't see that the methods get actually executed. I assume that the behaviour is supposed to be similar to that in controllers?

Excerpt from base\Action

    public function runWithParams($params)
    {
        if (!method_exists($this, 'run')) {
            throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
        }
        $args = $this->controller->bindActionParams($this, $params);
        Yii::trace('Running action: ' . get_class($this) . '::run()', __METHOD__);
        if (Yii::$app->requestedParams === null) {
            Yii::$app->requestedParams = $args;
        }
        return call_user_func_array([$this, 'run'], $args);
    }
@cebe
Copy link
Member

cebe commented Feb 26, 2014

@qiangxue why did you add them, what's the usecase for them? When implementing an action you have full control over the complete flow so they are not needed imo.

@ivokund
Copy link
Contributor Author

ivokund commented Feb 26, 2014

Imo beforeRun is convenient if (for example) you're using inheritance in action classes. You could also just override init, but it would help to have a method that's only executed after init and before running the action. Of course, this would be trivial to implement where specifically necessary.

I don't see any point in afterRun other than having a complete set of before/after methods :)

@qiangxue
Copy link
Member

The use case is you have a base action class and you want to put some common logic that should get executed before/after action run.

I added these methods when implementing web api support which needs to check if the current request method is allowed by the action.

@ivokund the issue is valid. Need to change runWithParams().

@qiangxue
Copy link
Member

Fixed. Thanks.

@ivokund
Copy link
Contributor Author

ivokund commented Feb 26, 2014

I think there's a typo in your fix

    public function runWithParams($params)
    {
        if (!method_exists($this, 'run')) {
            throw new InvalidConfigException(get_class($this) . ' must define a "run()" method.');
        }
        $args = $this->controller->bindActionParams($this, $params);
        Yii::trace('Running action: ' . get_class($this) . '::run()', __METHOD__);
        if (Yii::$app->requestedParams === null) {
            Yii::$app->requestedParams = $args;
        }
        if ($this->beforeRun()) {
            call_user_func_array([$this, 'run'], $args);
            $this->afterRun();
        }
    }

The result of run is not returned so external actions do not currently work any more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants