我在yii2中创建了一个名为AuthContoller的新控制器。但是控制器操作不起作用。您可以检查控制器我在做什么错吗?
控制器
<?php
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\AccessControl;
/**
* Site controller
*/
class AuthController extends Controller
{
public function beforeAction($action){
if (parent::beforeAction($action)){
if (!Yii::$app->user->isGuest){
return $this->redirect('home/');
}
}
}
public function actionLogin(){
echo 'I am here'; exit;
$this->layout = false;
return $this->render('login');
}
}
视图中没有任何内容
答案 0 :(得分:0)
根据Yii2文档,该函数必须返回true
或false
值。
添加return true;
将解决您的问题。
该方法将触发
EVENT_BEFORE_ACTION
事件。方法的返回值将确定操作是否应继续运行。
您的代码应如下所示:
public function beforeAction($action){
// your custom code here, if you want the code to run before action filters,
// which are triggered on the [[EVENT_BEFORE_ACTION]] event, e.g. PageCache or AccessControl
if (!parent::beforeAction($action)) {
return false;
}
if (!Yii::$app->user->isGuest){
return $this->redirect('home/');
}
// other custom code here
return true; // or false to not run the action
}