dsuvs66406 2015-07-16 13:08
浏览 47
已采纳

Yii2高级模板

I have installed and configured the yii2 advanced template. Everyone is saying that they want to separate frontend login from backend login, but I want to do the opposite.

What i want is, when I login to frontend, i should also remain logged in on backend side. I tried different configurations but when I login to frontend and go to backend area I'm a guest!

frontend: photography.dev backend: admin.photography.dev

By default everyone says that yii2 advanced template has the same login for both: frontend and backend, but in my case it isn't true.

EDIT: Updated with complete backend, frontend and common configs

Common:

return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'components' => [
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
],
];

Backend:

<?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,
];

Backend environment dev:

$config = [
'components' => [
    'request' => [
        // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
        'cookieValidationKey' => '',
    ],
],
];

Frontend:

<?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,
];

Frontend dev environment:

$config = [
'components' => [
    'request' => [
        // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
        'cookieValidationKey' => 'iGk90GAbgQg2jT5aQ5PMcG1A3A9E_iNq',
    ],
],
];
  • 写回答

2条回答 默认 最新

  • download92000 2015-07-16 23:27
    关注

    Make sure that the cookie uses the same settings in both the frontend as the backend part. Because your admin section is located in a subdomain and Yii takes that as the default domain value, you should also set the domain setting, like as follows:

    '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',
        ],
    ],
    

    The browser will always use the most specific cookie, so a cookie on admin.photography.dev would overrule a cookie on photography.dev

    Edit: if you want to change the csrf cookie as well, you can use this in frontend/config/main.php and backend/config/main.php:

    'request' => [
        'baseUrl'    => '',
        'csrfParam'  => '_csrf',
        'csrfCookie' => [
            'httpOnly' => true,
            'path'     => '/',
            'domain'   => 'photography.dev',
        ],
    ],
    

    If you do this, make sure that the cookieValidationKey in frontend/config/main-local.php and backend/config/main-local.php are the same.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行