Yii框架自定义403处理特定控制器

时间:2013-07-07 11:35:32

标签: yii

我有一项要求,即检查用户当前的贷款是否为付费会员,以便采取一系列措施。如果用户不是付费会员,那么他必须被发送到会员页面。以下是代码。在控制器(Yii框架)

public function accessRules()
{  return array(

        array('allow', 
            'actions'=>array('enroll','index','admin','suggesttags'),
            'users'=>array('@'),
            ),
        array('allow', 
            'actions'=>array('view', 'read'),
            'users'=>array(Yii::app()->user->name),
            'expression' => 'Yii::app()->controller->hasPaied()'
            ),

现在hasPayed()函数为非付费成员返回false,目前用户被重定向到403异常。

我想将403例外页面自定义为“获取会员资格”页面。有办法吗?以便从此特定controller\action引发的所有异常都发送到take members页面,其余403异常保持不变?

1 个答案:

答案 0 :(得分:1)

尝试使用CAccessControlerFilter中的 deniedCallback

// optional, the denied method callback name, that will be called once the
// access is denied, instead of showing the customized error message. It can also be
// a valid PHP callback, including class method name (array(ClassName/Object, MethodName)),
// or anonymous function (PHP 5.3.0+). The function/method signature should be as follows:
// function foo($user, $rule) { ... }
// where $user is the current application user object and $rule is this access rule.
// This option is available since version 1.1.11.
'deniedCallback'=>'redirectToDeniedMethod',