Mohsen
Mohsen

Reputation: 1422

afterAction and returning method in yii2?

i have controller in common folder that have method test

  public function actionTest() {
    $s = "sdfs";
    return $s;
}

And have afterAction for this action like:

   public function afterAction($action) {
    if ($action->id == "test") { 
       echo 'here afterActioin';
     }
   }

and i call the test action from another controller :

public function actionTest3() {
  echo Yii::$app->runAction('travia/test');
 }

the problem is when i call the test3 in browser nothing display but if i comment the afterAction the method will return . where is the problem what's the behaviour of the afterAction that doen't let my action to return something?

Upvotes: 2

Views: 8750

Answers (1)

Thao Ngo
Thao Ngo

Reputation: 3771

Yii2 docs

This method is invoked right after an action is executed. The method will trigger the EVENT_AFTER_ACTION event. The return value of the method will be used as the action return value. If you override this method, your code should look like the following:

public function afterAction($action, $result)
{
    $result = parent::afterAction($action, $result);
    // your custom code here
    return $result;
}

Upvotes: 6

Related Questions