3

如何覆盖模块的 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',
            )
        );
    }
            .......

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

4

3 回答 3

2

使用此代码


类 AgentModule 扩展 CWebModule {
    公共 $assetsUrl;
    公共 $defaultController = '登录';

    公共函数初始化(){

        // 创建模块时调用此方法
        $this->setComponents(数组(
                'errorHandler' => 数组(
                        'errorAction' => 'admin/login/error'),
                '用户' => 数组(
                        '类' => 'CWebUser',
                        'loginUrl' => Yii::app()->createUrl('admin/login'),
                )
                )
        );

        Yii::app()->user->setStateKeyPrefix('_admin');

        // 导入模块级模型和组件
        $this->setImport(数组(
                'admin.models.*',
                'admin.components.*',
                )
        );
    }

    公共函数 beforeControllerAction($controller, $action) {

        if(父::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();
            } 别的 {
                返回真;
            }
        }
        别的
            返回假;
    }
}
于 2013-06-13T10:45:51.433 回答
1

In 2nd code block in your question you defined user component for module AgentModule.

If i understand right, somewhere in your view or controller you use Yii::app()->user->loginUrl, right? To get url defined in AgentModule you should use Yii::app()->user->controller->module->user but only while in AgentModule context.

Easiest way to make it different is to define somewhere login urls for module, and for app, and just use this predefined urls.

Try reading about module compoents here: http://www.yiiframework.com/wiki/27/how-to-access-a-component-of-a-module-from-within-the-module-itself/

于 2013-02-05T18:02:15.180 回答
0
类 WebUser 扩展 CWebUser {

    公共 $module = 数组();

    公共函数初始化(){
        父::init();
        if(isset($this->module['loginUrl'])){
            if(!isset($this->module['moduleId'])){
                throw new Exception('moduleId 必须定义');
            }别的{
                $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']);
                }
            }
        }

    }

}
之后,您需要在配置文件下进行一些更改,即
返回数组(
    '组件' => 数组(
        '用户' => 数组(
            'allowAutoLogin' => 真,
            '类' => '网络用户',
            '模块' => 数组(
                'loginUrl' => '/r_admin',
                'moduleId' => 'r_admin'
            )
        ),
    )
);
于 2016-03-26T04:39:10.447 回答