diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index b29456ce12..8dc16b6761 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -205,7 +205,7 @@ Yii Framework 2 Change Log - Renamed `yii\web\User::authTimeoutVar` to `authTimeoutParam` - Renamed `yii\web\User::returnUrlVar` to `returnUrlParam` - Chg: Added `View::viewFile` and removed `ViewEvent::viewFile` (qiangxue) - +- Chg: Changed `Controller::afterAction()`, `Module::afterAction()` and `ActionFilter::afterAction()` to pass `$result` by value instead of reference (qiangxue) - New #66: [Auth client library](https://github.com/yiisoft/yii2-authclient) OpenId, OAuth1, OAuth2 clients (klimov-paul) - New #706: Added `yii\widgets\Pjax` and enhanced `GridView` to work with `Pjax` to support AJAX-update (qiangxue) - New #1393: [Codeception testing framework integration](https://github.com/yiisoft/yii2-codeception) (Ragazzo) diff --git a/framework/base/ActionFilter.php b/framework/base/ActionFilter.php index 3b8f8ebd91..6fb114ca03 100644 --- a/framework/base/ActionFilter.php +++ b/framework/base/ActionFilter.php @@ -65,7 +65,7 @@ class ActionFilter extends Behavior public function afterFilter($event) { if ($this->isActive($event->action)) { - $this->afterAction($event->action, $event->result); + $event->result = $this->afterAction($event->action, $event->result); } } @@ -85,9 +85,11 @@ class ActionFilter extends Behavior * You may override this method to do some postprocessing for the action. * @param Action $action the action just executed. * @param mixed $result the action execution result + * @return mixed the processed action result. */ - public function afterAction($action, &$result) + public function afterAction($action, $result) { + return $result; } /** diff --git a/framework/base/Controller.php b/framework/base/Controller.php index 333c99a2ae..27d234512b 100644 --- a/framework/base/Controller.php +++ b/framework/base/Controller.php @@ -126,11 +126,12 @@ class Controller extends Component implements ViewContextInterface Yii::$app->trigger(Application::EVENT_BEFORE_ACTION, $event); if ($event->isValid && $this->module->beforeAction($action) && $this->beforeAction($action)) { $result = $action->runWithParams($params); - $this->afterAction($action, $result); - $this->module->afterAction($action, $result); + $result = $this->afterAction($action, $result); + $result = $this->module->afterAction($action, $result); $event = new ActionEvent($action); - $event->result = &$result; + $event->result = $result; Yii::$app->trigger(Application::EVENT_AFTER_ACTION, $event); + $result = $event->result; } $this->action = $oldAction; return $result; @@ -222,14 +223,17 @@ class Controller extends Component implements ViewContextInterface * This method is invoked right after an action is executed. * You may override this method to do some postprocessing for the action. * If you override this method, please make sure you call the parent implementation first. + * Also make sure you return the action result, whether it is processed or not. * @param Action $action the action just executed. * @param mixed $result the action return result. + * @return mixed the processed action result. */ - public function afterAction($action, &$result) + public function afterAction($action, $result) { $event = new ActionEvent($action); - $event->result = &$result; + $event->result = $result; $this->trigger(self::EVENT_AFTER_ACTION, $event); + return $event->result; } /** diff --git a/framework/base/Module.php b/framework/base/Module.php index b65ad8b5d1..c82716fb98 100644 --- a/framework/base/Module.php +++ b/framework/base/Module.php @@ -658,10 +658,13 @@ class Module extends Component * This method is invoked right after an action of this module has been executed. * You may override this method to do some postprocessing for the action. * Make sure you call the parent implementation so that the relevant event is triggered. + * Also make sure you return the action result, whether it is processed or not. * @param Action $action the action just executed. * @param mixed $result the action return result. + * @return mixed the processed action result. */ - public function afterAction($action, &$result) + public function afterAction($action, $result) { + return $result; } } diff --git a/framework/web/PageCache.php b/framework/web/PageCache.php index 4c8cc508c4..08857d530b 100644 --- a/framework/web/PageCache.php +++ b/framework/web/PageCache.php @@ -136,15 +136,12 @@ class PageCache extends ActionFilter } /** - * This method is invoked right after an action is executed. - * You may override this method to do some postprocessing for the action. - * @param Action $action the action just executed. - * @param mixed $result the action execution result + * @inheritdoc */ - public function afterAction($action, &$result) + public function afterAction($action, $result) { echo $result; $this->view->endCache(); - $result = ob_get_clean(); + return ob_get_clean(); } }