Override Yii login address for module

How to override the Yii url for a module?

here is the basic configuration of the base application:

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

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

      

And then I have an agent module, I want the login url to be different in this module, and the 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 it doesn't work. Please help ... (TT)

+3


source to share


3 answers


In the second block of code in your question, you defined a component user

for a module AgentModule

.

If I understand correctly, somewhere in your view or controller you are using Yii::app()->user->loginUrl

, right? To get the url defined in AgentModule

, you must use Yii::app()->user->controller->module->user

, but only in AgentModule

context.



The easiest way to do it differently is to define the module login URL and for the application somewhere, and just use those predefined URLs.

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

+1


source


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;
    }
}
+2


source


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 ie 
return array (
    'components' => array (
        'user' => array (
            'allowAutoLogin' => true,
            'class' => 'WebUser',
            'module' => array (
                'loginUrl' => '/ r_admin',
                'moduleId' => 'r_admin'
            )
        ),
    )
);
0


source







All Articles