繁体   English   中英

Yii2高级模板

[英]Yii2 Advanced Template

我已经安装并配置了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',
    ],
],
];

确保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相同。

您必须为两个应用程序使用相同的会话名称,登录名才能应用于两个应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM