phpyiiyii-modules

Override Yii Login Url for Module


How to override Yii Login URL Modul for module?

here is the main configuration for base application:

return array(
            .........

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

And then i have agent module, i want in this this module the login URL is different and login method is also different.

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',
            )
        );
    }
            .......

But i don't know why this not work. Please help... (T.T)


Solution

  • 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/