Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 731
GusDeCooL Avatar asked Nov 28 '25 05:11

GusDeCooL


1 Answers

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;
    }
}
like image 78
Abhishek Avatar answered Nov 30 '25 17:11

Abhishek