4
我现在正在与yii2抗争。以下情景:Yii2:是否可以从后端打开前端会话?
我使用yii2高级模板,并具有前端和后端,并具有单独的用户表和登录名。
现在我正在寻找一种后端用户可以从后端以前端用户身份登录的方式。假设您位于后端并查看前端用户,则可以单击“以此用户身份登录”。
这种情况可能吗?
我试图配置后端的配置一个前端使用:
'user' => [
'identityClass' => 'backend\models\BackendUser',
'enableAutoLogin' => false,
],
'frontendUser' => [
'class' => 'yii\web\User',
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
],
,并在我的控制器我尝试这样:
后端配置:
if (Yii::$app->frontendUser->login($user_group->user, 0)) {
return $this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/index', 'client' => $client->login_address]));
}
编辑谢尔盖的回答后,
'user' => [
'identityClass' => 'backend\models\BackendUser',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_backendUser', // unique for backend
]
],
前端配置:
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'loginUrl' => ['message/welcome'], // weil beim SessionTimeout darauf umgeleitet wird,
'authTimeout' => 1800,
'identityCookie' => [
'name' => '_frontendUser', // unique for frontend
]
],
控制器功能:
public function actionLoginAs($id)
{
$user_group = UserGroup::findOne($id);
if (is_null($user_group)) {
return $this->redirect(['site/index']);
}
$group = $user_group->group;
$client = $group->client;
$yiiuser = new yii\web\User([
'identityClass' => 'common\models\User',
'identityCookie' => [
'name' => '_frontendUser', // unique for frontend
]
]);
$user = $user_group->user;
if ($yiiuser->login($user, 15 * 60)) {
return $this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/index', 'client' => $client->login_address]));
}
}
感谢您的回答。问题仍然是前端没有Yii :: $ app-> user并且现在抛出一个错误 –
@SarahWest,你是否在前端配置中添加了'user'组件? – Sergey
@SarahWest,有什么样的错误?你能显示错误信息吗? – Sergey