Yii框架 - 如何为模块的所有控制器指定相同的访问规则?

时间:2009-10-23 10:01:08

标签: php module yii

我为管理员特定的操作创建了模块。我不想为每个控制器编写相同的访问规则,它不是很好的编码风格。

4 个答案:

答案 0 :(得分:3)

一种解决方案是为每个经过身份验证的类扩展一个通用的BaseClass控制器。

这样你就可以写一次。

答案 1 :(得分:3)

模块就像一个具有独立目录结构的子应用程序。它不负责过滤或检查权限。

唯一重要的解决方案是定义Ismael提出的新抽象。

class ExtendedController
{
    public function rules()
    {
        return array_merge(parent::rules(), array(
           // your rules
        ));
    }
}

答案 2 :(得分:1)

Ismael和pestaa的选择非常好,甚至快速实施,但我总是推荐更强大的替代品,如RBAC模型。您可以在Yii RBAC

中找到http://code.google.com/p/srbac/的非常好的GUI

答案 3 :(得分:0)

这对我有用:

class extendedController extends baseController
{
    public function accessRules()
    {
        return array_merge(

            // This controller's rules are added first:
            // Allow all users to call the 'hello' action.
            array(
                array('allow',
                    'actions'=>array('hello'), 
                    'users'=>array('*'),
                ),
            ),

            // BaseController rules are added last, especially
            // if the last rule in the baseController denies all
            // users that were not allowed yet.
            parent::accessRules()
        );
    }

    public function actionHello()
    {
        echo('Hello!');
    }
}
相关问题