gpt4 book ai didi

php - yii2 中的 afterAction 和返回方法?

转载 作者:行者123 更新时间:2023-12-02 17:31:47 24 4
gpt4 key购买 nike

我在具有方法测试的公共(public)文件夹中有 Controller

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

并为此操作设置 afterAction,例如:

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

然后我从另一个 Controller 调用测试操作:

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

问题是当我在浏览器中调用 test3 时没有任何显示,但是如果我评论 afterAction 该方法将返回。问题出在哪里,不允许我的操作返回某些内容的 afterAction 的行为是什么?

最佳答案

Yii2 文档

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;
}

关于php - yii2 中的 afterAction 和返回方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32220472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com