簡體   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