覆盖模块的 Yii 登录网址


Override Yii Login Url for Module

如何覆盖模块的 Yii 登录 URL 模块?

以下是基本应用程序的主要配置:

return array(
            .........
    // application components
    'components'=>array(
        'user'=>array(              
            // enable cookie-based authentication
            'allowAutoLogin'=>true,
            'loginUrl' => '/site/login',
        ),
           ..........
      );

然后我有代理模块,我希望在这个模块中登录 URL 不同,登录方法也不同。

class AgentModule extends CWebModule {
    public function init() {
        // this method is called when the module is being created
        // you may place code here to customize the module or the application
        // import the module-level models and components
        $this->setImport(array(
            'agent.models.*',
            'agent.components.*',
        ));
        $this->defaultController = 'default';
        $this->layoutPath = Yii::getPathOfAlias('agent.views.layout');
        $this->components = array(
            'user' => array(
                'class' => 'AgentUserIdentity',
                'loginUrl' => '/agent/default/login',
            )
        );
    }
            .......

但我不知道为什么这不起作用。请帮忙...(T.T)

使用此代码

类代理模块扩展 CWebModule {    公共$assetsUrl;    公共$defaultController ="登录";    公共函数 init() {        创建模块时调用此方法        $this->setComponents(array(                'errorHandler' => array(                        'errorAction' => 'admin/login/error'),                'user' => array(                        'class' => 'CWebUser',                        'loginUrl' => Yii::app()->createUrl('admin/login'),                )                )        );        Yii::app()->user->setStateKeyPrefix('_admin');        导入模块级模型和组件        $this->setImport(array(                'admin.models.*',                'admin.components.*',                )        );    }    public function beforeControllerAction($controller, $action) {        if(parent::beforeControllerAction($controller, $action)) {            在任何模块控制器之前调用此方法            执行操作            $route = $controller->id 。'/' .$action->id;$publicPages = 数组(                    "登录/登录",                    "登录/错误",            );            if (Yii::app()->user->name !== 'admin' && !in_array($route,                              $publicPages)) {                Yii::app()->getModule('admin')->user->loginRequired();            } else {                返回真;            }        }        还            返回假;    }}

在问题的第二个代码块中,您为模块AgentModule定义了user组件。

如果我理解正确,在您的视图或控制器中的某个地方您使用Yii::app()->user->loginUrl,对吧?要获取在AgentModule中定义的 url,您应该使用 Yii::app()->user->controller->module->user只能在AgentModule上下文中使用。

使其与众不同的最简单方法是为模块和应用程序定义登录 URL,然后仅使用此预定义 URL。

尝试在此处阅读有关模块组合的信息:http://www.yiiframework.com/wiki/27/how-to-access-a-component-of-a-module-from-within-the-module-itself/

类 WebUser 扩展 CWebUser {    公共$module = 数组();    公共函数 init() {        父::初始化();        if(isset($this->module['loginUrl'])){            if(!isset($this->module['moduleId'])){                抛出新的异常('模块 ID 必须定义');            }else{                $moduleId = Yii::app()->controller->module->id;                if($moduleId == $this->module['moduleId']){                    #$this->loginUrl = Yii::app()->request->redirect(Yii::app()->createUrl($this->module['loginUrl']));                    $this->loginUrl = array($this->module['loginUrl']);                }            }        }    }}
之后,您需要在配置文件下进行一些更改,即返回数组(    'components' => array(        'user' => array(            '允许自动登录' => true,            'class' => 'WebUser',            '模块' => 数组(                'loginUrl' => '/r_admin',                'moduleId' => 'r_admin'            )        ),    ));