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を拡張します{
    public $ assertsUrl;
    public $ defaultController='ログイン';

    パブリック関数init(){

        //このメソッドは、モジュールの作成時に呼び出されます
        $ this-> setComponents(array(
                'errorHandler' => array(
                        'errorAction' =>'admin / login / error')、
                'user' => array(
                        'class' =>'CWebUser'、
                        'loginUrl' => Yii :: app()-> createUrl('admin / login')、
                )。
                )。
        );

        Yii :: app()-> user-> setStateKeyPrefix('_ admin');

        //モジュールレベルのモデルとコンポーネントをインポートします
        $ this-> setImport(array(
                'admin.models。*'、
                'admin.components。*'、
                )。
        );
    }

    public function beforeControllerAction($ controller、$ action){

        if(parent :: beforeControllerAction($ controller、$ action)){
            //このメソッドは、モジュールコントローラーの前に呼び出されます
            //アクションが実行されます
            $ route = $controller->id。'/'。$ action-> id;


            $ publicPages = array(
                    'login / login'、
                    'ログインエラー'、
            );

            if(Yii :: app()-> user-> name!=='admin' &&!in_array($ route、
                              $ publicPages)){
                Yii :: app()-> getModule('admin')-> user-> loginRequired();
            } そうしないと {
                trueを返します。
            }
        }
        そうしないと
            falseを返します。
    }
}
于 2013-06-13T10:45:51.433 に答える
1

質問の2番目のコードブロックで、userモジュールのコンポーネントを定義しましAgentModuleた。

私が正しく理解していれば、あなたが使用しているビューまたはコントローラーのどこかでYii::app()->user->loginUrl正しいですか?で定義されたURLを取得するには、コンテキスト内でのみAgentModule使用する必要Yii::app()->user->controller->module->userがあります。AgentModule

これを変える最も簡単な方法は、モジュールとアプリのログインURLをどこかに定義し、この事前定義されたURLを使用することです。

ここでモジュールコンポーネントについて読んでみてください: 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を拡張します{

    public $ module = array();

    パブリック関数init(){
        parent :: init();
        if(isset($ this-> module ['loginUrl'])){
            if(!isset($ this-> module ['moduleId'])){
                新しい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']);
                }
            }
        }

    }

}
その後、設定ファイルの下でいくつかの変更を行う必要があります。
配列を返す
    'コンポーネント'=>array(
        'user' => array(
            'allowAutoLogin' => true、
            'class' =>'WebUser'、
            'モジュール'=>array(
                'loginUrl' =>'/ r_admin'、
                'moduleId' =>'r_admin'
            )。
        )、
    )。
);
于 2016-03-26T04:39:10.447 に答える