如何覆盖模块的 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/