在yii里面还有beforeControllerAction()这个函数,但是到了yii2里面就没了,但是不要着急,这个功能还在,改成了beforeAction()
namespace app\modules\back;
class Back extends \yii\base\Module
{
public $controllerNamespace = 'app\modules\back\controllers';
public function init()
{
parent::init();
// custom initialization code goes here
//\Yii::$app->user->enableSession = false;
}
public function beforeAction($action)
{
if (!parent::beforeAction($action)) {
return false;
}
// your custom code here
return true; // or false to not run the action
}
}
本文介绍了在Yii2框架中如何使用beforeAction方法替代之前的beforeControllerAction功能。通过具体代码示例展示了如何在模块级别定义beforeAction方法来实现对所有控制器动作前的操作预处理。
1万+

被折叠的 条评论
为什么被折叠?



