Question

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)

Was it helpful?

Solution 2

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/

OTHER TIPS

Use this code


class AgentModule extends CWebModule {
    public $assetsUrl;
    public $defaultController = 'Login';

    public function init() {

        // this method is called when the module is being created
        $this->setComponents(array(
                'errorHandler' => array(
                        'errorAction' => 'admin/login/error'),
                'user' => array(
                        'class' => 'CWebUser',
                        'loginUrl' => Yii::app()->createUrl('admin/login'),
                )
                )
        );

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

        // import the module-level models and components
        $this->setImport(array(
                'admin.models.*',
                'admin.components.*',
                )
        );
    }

    public function beforeControllerAction($controller, $action) {

        if(parent::beforeControllerAction($controller, $action)) {
            // this method is called before any module controller 
            //action is performed
            $route = $controller->id . '/' . $action->id;


            $publicPages = array(
                    'login/login',
                    'login/error',
            );

            if (Yii::app()->user->name !== 'admin' && !in_array($route, 
                              $publicPages)) {
                Yii::app()->getModule('admin')->user->loginRequired();
            } else {
                return true;
            }
        }
        else
            return false;
    }
}
class WebUser extends CWebUser {

    public $module = array();

    public function init() {
        parent::init();
        if(isset($this->module['loginUrl'])){
            if(!isset($this->module['moduleId'])){
                throw new Exception('moduleId Must defined');
            }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']);
                }
            }
        }

    }

}
after that you need to do some changes under config file i.e 
return array(
    'components' => array(
        'user' => array(
            'allowAutoLogin' => true,
            'class' => 'WebUser',
            'module' => array(
                'loginUrl' => '/r_admin',
                'moduleId' => 'r_admin'
            )
        ),
    )
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top