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

有帮助吗?

解决方案

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

这样你就可以写一次。

其他提示

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

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

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

Ismael和pestaa的选择非常好,甚至快速实施,但我总是推荐更强大的替代品,如RBAC模型。您可以在 Yii RBAC 的非常好的GUI /code.google.com/p/srbac/"rel =“nofollow noreferrer”> http://code.google.com/p/srbac/

这对我有用:

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!');
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top