我已经安装并配置了yii2高级模板。每个人都说他们希望将前端登录与后端登录分开,但我想做相反的事情。
我想要的是,当我登录前端时,我还应该保持在后端登录。我尝试了不同的配置,但是当我登录前端并前往后端区域时我是客人!
前端:photography.dev 后端:admin.photography.dev
默认情况下,每个人都说yii2高级模板具有相同的登录名:前端和后端,但在我的情况下,它不是真的。
编辑:更新了完整的后端,前端和常见配置
普通的:
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
],
];
后端:
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'enableSession' => true,
'idParam' => '_user',
'identityCookie' => [
'name' => '_user',
'path'=>'/'
]
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'/' => 'site/index',
'<alias:login|logout|about|contact>' => 'site/<alias>'
]
],
'urlManagerFrontEnd' => [
'class' => 'yii\web\urlManager',
'baseUrl' => 'http://photography.dev',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
];
后端环境开发:
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => '',
],
],
];
前端:
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php'));
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'frontend\controllers',
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'enableSession' => true,
'idParam' => '_user',
'identityCookie' => [
'name' => '_user',
'path'=>'/'
]
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'/' => 'site/index',
'index' => 'site/index',
'<alias:login|logout|about|contact|index>' => 'site/<alias>'
],
],
'urlManagerBackend' => [
'class' => 'yii\web\urlManager',
'baseUrl' => 'http://admin.photography.dev',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
];
前端开发环境:
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'iGk90GAbgQg2jT5aQ5PMcG1A3A9E_iNq',
],
],
];
答案 0 :(得分:1)
确保cookie在前端和后端部分使用相同的设置。由于您的管理部分位于子域中,而Yii将其作为默认域值,因此您还应设置域设置,如下所示:
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_identity',
'path' => '/',
'httpOnly' => true,
'domain' => 'photography.dev',
],
],
'session' => [
'name' => 'PHPFRONTENDBACKENDSESSID',
'cookieParams' => [
'httpOnly' => true,
'path' => '/',
'domain' => 'photography.dev',
],
],
浏览器将始终使用最具体的cookie,因此admin.photography.dev上的cookie会覆盖photography.dev上的cookie
编辑:如果你想更改csrf cookie,可以在frontend / config / main.php和backend / config / main.php中使用它:
'request' => [
'baseUrl' => '',
'csrfParam' => '_csrf',
'csrfCookie' => [
'httpOnly' => true,
'path' => '/',
'domain' => 'photography.dev',
],
],
如果这样做,请确保frontend / config / main-local.php和backend / config / main-local.php中的cookieValidationKey是相同的。
答案 1 :(得分:0)
您必须为两个应用程序使用相同的会话名称才能将登录应用于这两个应用程序。