【问题标题】:Yii: init module when it's not in configYii:当它不在配置中时初始化模块
【发布时间】:2014-06-26 20:12:15
【问题描述】:

我的 Yii 项目中有一些模块,我想仅在当前用户有权使用该模块时才初始化它们。

这就是我所做的:在 SiteController 中,我获取包含用户模块关系的 db 表,并获得一组模块名称。然后我想在配置中没有定义模块时加载每个可用模块。我找不到任何操作员或方法来做到这一点。有没有办法在不解决问题的情况下实现这一点?

我想到的另一件事是在每个模块的 beforeControllerAction 中取消模块初始化,但我仍然无法解决这个问题。

【问题讨论】:

    标签: php yii


    【解决方案1】:

    您最好使用beforeControllerAction 来检查访问权限,如下所示:

    public function beforeControllerAction($controller, $action)
    {
       if(parent::beforeControllerAction($controller, $action))
       {
          // this method is called before any module controller action is performed
          // you may place customized code here              
          if(!Yii::app()->user->checkAccess('getMyModule')){
              throw new CHttpException(403, Yii::t('yii','You do not have sufficient permissions to access.'));
          }             
          return true;
       }
       else
          return false;
    }
    

    【讨论】:

    • 谢谢!如果我有少量模块,这是一个很好的解决方案。但是如果我有 100 多个模块(现在不是 :-))我宁愿只加载允许的 5-15 个。问题是我可以只定义需要的模块,还是我都定义了?
    • @Switcher 模块不会一次全部加载。 Yii 使用延迟加载,模块文件只会在请求后加载。
    • 哦,好的。我以前从未尝试过使用它们。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2016-02-04
    • 1970-01-01
    相关资源
    最近更新 更多