如何在 Yii 框架中为不同的模块正确创建不同的登录
How to properly create different logins for different Modules in Yii framework
在我的 Yii 1.x 应用程序中,我定义了新的 Admin
模块。在 admin
模块的 init
方法中,我定义了新的 user
组件,如下所示:
$this->setComponents(array(
'user'=>array(
'class' => 'CWebUser',
// enable cookie-based authentication
'allowAutoLogin'=>true,
'baseUrl'=>Yii::app()->createUrl("admin/user/login"),
'stateKeyPrefix' => '_admin',
),
));
现在,我希望我可以做到以下几点:
Yii::app()->getModule("admin")->user->login($this->_identity,$duration)
或
Yii::app()->getModule("admin")->user->logout();
但它不起作用。
当我打印我的模块 (var_dump(Yii::app()>getModule("admin"))
) 时,我可以看到 user
组件未定义。
object(AdminModule)[14]
public 'defaultController' => string 'default' (length=7)
public 'layout' => null
public 'controllerNamespace' => null
public 'controllerMap' =>
array (size=0)
empty
private '_controllerPath' (CWebModule) => null
private '_viewPath' (CWebModule) => null
private '_layoutPath' (CWebModule) => null
public 'preload' =>
array (size=0)
empty
public 'behaviors' =>
array (size=0)
empty
private '_id' (CModule) => string 'admin' (length=10)
private '_parentModule' (CModule) => null
private '_basePath' (CModule) => string '/srv/www/htdocs/public/project/application/protected/modules/admin' (length=71)
private '_modulePath' (CModule) => null
private '_params' (CModule) => null
private '_modules' (CModule) =>
array (size=0)
empty
private '_moduleConfig' (CModule) =>
array (size=0)
empty
private '_components' (CModule) =>
array (size=0)
empty
private '_componentConfig' (CModule) =>
array (size=1)
'user' =>
array (size=4)
'class' => string 'CWebUser' (length=8)
'allowAutoLogin' => boolean true
'baseUrl' => string '/project/application/index.php/admin/user/login' (length=52)
'stateKeyPrefix' => string '_admin' (length=11)
private '_e' (CComponent) => null
private '_m' (CComponent) => null
默认方法setComponents
(setComponent
) 检查组件是否已经定义,它只将新参数与旧参数合并。
所以你必须将第二个参数设置为 false
以重新定义新组件(组件):
public function init() {
parent::init();
$this->setComponents(array(
'user'=>array(
'class' => 'CWebUser',
// enable cookie-based authentication
'allowAutoLogin'=>true,
'baseUrl'=>Yii::app()->createUrl("admin/user/login"),
'stateKeyPrefix' => '_admin',
),
), false);
}
By the way, inside the module you can use component without calling
module:
Yii::app()->user->login($this->_identity,$duration);
Yii::app()->user->logout();
拜托,来自工作项目的代码。
在模块 main class:
中定义的用户组件
public function init() {
parent::init();
$this->setImport(array(
'admin.models.*',
'admin.components.*',
));
Yii::app()->setComponents(array(
'user'=>array(
'class'=>'AdminWebUser',
'allowAutoLogin'=>true,
'loginRequiredAjaxResponse'=>'Dear admin, your session expired, login and try again',
'stateKeyPrefix'=>'admin_',
'authTimeout'=>14400,
),
), false);
}
和主配置文件中定义的默认用户组件:
'user'=>array(
'class'=>'WebUser',
'loginRequiredAjaxResponse'=>'Your session expired, login and try again',
'autoRenewCookie'=>false,
'allowAutoLogin'=>false,
'stateKeyPrefix'=>'user_',
'loginUrl'=>'/home/login',
),
因此您可以 "have two sessions for one user" 取决于模块(在其他会话模块中,在 root -other 中)。例如 var_dump(Yii::app()->user->stateKeyPrefix)
模块外给出 user_
,但模块内给出 admin_
。
实际上 yii 使用相同的会话文件,但设置和获取数据取决于 stateKeyPrefix
(当我们使用 setState()
或 getState()
时)。
因此,如果您使用 Yii::app()->user->setState('userName', 'John')
在 admin
模块中保留 userName
,它会放入会话文件 'admin_userName' => 'John'
,这意味着您可以从 admin
模块(例如在 root 中)通过使用 Yii::app()->session->get('admin_userName')
。
如果您尝试使用 getState
获取它(在模块外),它不能 return 您的正确值,因为它必须找到 user_userName
(因为在 root 中,对于用户组件 'stateKeyPrefix' => 'user_'
) 而不是 admin_userName
.
感谢您,抱歉表达太长))
在我的 Yii 1.x 应用程序中,我定义了新的 Admin
模块。在 admin
模块的 init
方法中,我定义了新的 user
组件,如下所示:
$this->setComponents(array(
'user'=>array(
'class' => 'CWebUser',
// enable cookie-based authentication
'allowAutoLogin'=>true,
'baseUrl'=>Yii::app()->createUrl("admin/user/login"),
'stateKeyPrefix' => '_admin',
),
));
现在,我希望我可以做到以下几点:
Yii::app()->getModule("admin")->user->login($this->_identity,$duration)
或
Yii::app()->getModule("admin")->user->logout();
但它不起作用。
当我打印我的模块 (var_dump(Yii::app()>getModule("admin"))
) 时,我可以看到 user
组件未定义。
object(AdminModule)[14]
public 'defaultController' => string 'default' (length=7)
public 'layout' => null
public 'controllerNamespace' => null
public 'controllerMap' =>
array (size=0)
empty
private '_controllerPath' (CWebModule) => null
private '_viewPath' (CWebModule) => null
private '_layoutPath' (CWebModule) => null
public 'preload' =>
array (size=0)
empty
public 'behaviors' =>
array (size=0)
empty
private '_id' (CModule) => string 'admin' (length=10)
private '_parentModule' (CModule) => null
private '_basePath' (CModule) => string '/srv/www/htdocs/public/project/application/protected/modules/admin' (length=71)
private '_modulePath' (CModule) => null
private '_params' (CModule) => null
private '_modules' (CModule) =>
array (size=0)
empty
private '_moduleConfig' (CModule) =>
array (size=0)
empty
private '_components' (CModule) =>
array (size=0)
empty
private '_componentConfig' (CModule) =>
array (size=1)
'user' =>
array (size=4)
'class' => string 'CWebUser' (length=8)
'allowAutoLogin' => boolean true
'baseUrl' => string '/project/application/index.php/admin/user/login' (length=52)
'stateKeyPrefix' => string '_admin' (length=11)
private '_e' (CComponent) => null
private '_m' (CComponent) => null
默认方法setComponents
(setComponent
) 检查组件是否已经定义,它只将新参数与旧参数合并。
所以你必须将第二个参数设置为 false
以重新定义新组件(组件):
public function init() {
parent::init();
$this->setComponents(array(
'user'=>array(
'class' => 'CWebUser',
// enable cookie-based authentication
'allowAutoLogin'=>true,
'baseUrl'=>Yii::app()->createUrl("admin/user/login"),
'stateKeyPrefix' => '_admin',
),
), false);
}
By the way, inside the module you can use component without calling module:
Yii::app()->user->login($this->_identity,$duration);
Yii::app()->user->logout();
拜托,来自工作项目的代码。 在模块 main class:
中定义的用户组件public function init() {
parent::init();
$this->setImport(array(
'admin.models.*',
'admin.components.*',
));
Yii::app()->setComponents(array(
'user'=>array(
'class'=>'AdminWebUser',
'allowAutoLogin'=>true,
'loginRequiredAjaxResponse'=>'Dear admin, your session expired, login and try again',
'stateKeyPrefix'=>'admin_',
'authTimeout'=>14400,
),
), false);
}
和主配置文件中定义的默认用户组件:
'user'=>array(
'class'=>'WebUser',
'loginRequiredAjaxResponse'=>'Your session expired, login and try again',
'autoRenewCookie'=>false,
'allowAutoLogin'=>false,
'stateKeyPrefix'=>'user_',
'loginUrl'=>'/home/login',
),
因此您可以 "have two sessions for one user" 取决于模块(在其他会话模块中,在 root -other 中)。例如 var_dump(Yii::app()->user->stateKeyPrefix)
模块外给出 user_
,但模块内给出 admin_
。
实际上 yii 使用相同的会话文件,但设置和获取数据取决于 stateKeyPrefix
(当我们使用 setState()
或 getState()
时)。
因此,如果您使用 Yii::app()->user->setState('userName', 'John')
在 admin
模块中保留 userName
,它会放入会话文件 'admin_userName' => 'John'
,这意味着您可以从 admin
模块(例如在 root 中)通过使用 Yii::app()->session->get('admin_userName')
。
如果您尝试使用 getState
获取它(在模块外),它不能 return 您的正确值,因为它必须找到 user_userName
(因为在 root 中,对于用户组件 'stateKeyPrefix' => 'user_'
) 而不是 admin_userName
.
感谢您,抱歉表达太长))