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