php - 覆盖模块的 Yii 登录 URL

标签 php yii yii-modules

如何覆盖模块的 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',
            )
        );
    }
            .......

但我不知道为什么这不起作用。 请帮忙...(T.T)

最佳答案

使用此代码



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

关于php - 覆盖模块的 Yii 登录 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14672165/

相关文章:

php - 在 Yii 中的 dropDownList 上生成不带 optgroup 的选择

php - Yii 2 : Using module's image declared as asset

php - 如何在 PHP 中运行 shell 脚本?

php - Yii ActiveRecord : select only first row with the same value

php - 在 laravel 5.2 中使用基于 api_token 的身份验证时出现 NotFoundHttpException

javascript - 有效地验证页面上的数据自加载以来没有被其他人更改过?

yii - 如何为 Yii 模块中的所有 Controller 设置布局?

PHP 多维数组用 KEYS 展平

javascript - 如何阅读blockadblock脚本?