Frameworks Yii框架:访问规则无法正常工作
我在访问控制方面有问题。我有规则:Frameworks Yii框架:访问规则无法正常工作,frameworks,yii,Frameworks,Yii,我在访问控制方面有问题。我有规则: array('deny', 'actions'=>array('index'), 'expression'=>'Yii::app()->user->isRegistered()', 'deniedCallback' => array( $this->render('//site/info',array( 'message'=>'You must acti
array('deny',
'actions'=>array('index'),
'expression'=>'Yii::app()->user->isRegistered()',
'deniedCallback' => array(
$this->render('//site/info',array(
'message'=>'You must activate your account.'
)
),Yii::app()->end()),
),
public function isRegistered()
{
return (Yii::app()->user->isGuest) ? FALSE : $this->level == 1;
}
功能:
array('deny',
'actions'=>array('index'),
'expression'=>'Yii::app()->user->isRegistered()',
'deniedCallback' => array(
$this->render('//site/info',array(
'message'=>'You must activate your account.'
)
),Yii::app()->end()),
),
public function isRegistered()
{
return (Yii::app()->user->isGuest) ? FALSE : $this->level == 1;
}
如果我以管理员身份登录,并且级别为3,isRegistered()返回false,但deniedCalback会运行
如何将其更改为仅在表达式为true时运行回调?您需要将回调指定为。按照您编写它的方式,它将始终执行该数组中的代码。您最好在控制器中编写一个专用方法
array('deny',
'actions'=>array('index'),
'expression'=>'Yii::app()->user->isRegistered()',
'deniedCallback' => array($this, 'accessDenied'),
),
// ...
public function accessDenied()
{
$this->render('//site/info', array(
'message' => 'You must activate your account.'
));
Yii::app()->end(); // not really neccessary
}
您需要将回调指定为。按照您编写它的方式,它将始终执行该数组中的代码。您最好在控制器中编写一个专用方法
array('deny',
'actions'=>array('index'),
'expression'=>'Yii::app()->user->isRegistered()',
'deniedCallback' => array($this, 'accessDenied'),
),
// ...
public function accessDenied()
{
$this->render('//site/info', array(
'message' => 'You must activate your account.'
));
Yii::app()->end(); // not really neccessary
}