Yii2 Advanced template: Same session between frontend and backend

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.

I want, when I login to frontend, to 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[dot]dev backend: admin[dot]photography[dot]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.

Backend:


'components' => [

    /*'urlManager' => [

        'enablePrettyUrl' => true,

        'showScriptName' => false,

        'rules' => [

            '/' => 'site/index',

            '<alias:login|logout|about|contact>' => 'site/<alias>'

        ]

    ],

    'urlManagerFrontEnd' => [

        'class' => 'yii\web\urlManager',

        'baseUrl' => 'photography[dot]dev',

        'enablePrettyUrl' => true,

        'showScriptName' => false,

    ],*/

    'user' => [

        'identityClass' => 'common\models\User',

        'enableAutoLogin' => true,

        'enableSession' => true,

    ],

    'log' => [

        'traceLevel' => YII_DEBUG ? 3 : 0,

        'targets' => [

            [

                'class' => 'yii\log\FileTarget',

                'levels' => ['error', 'warning'],

            ],

        ],

    ],

    'errorHandler' => [

        'errorAction' => 'site/error',

    ],

],

Frontend:


    'components' => [

    /*'urlManager' => [

        'enablePrettyUrl' => true,

        'showScriptName' => false,

        'rules' => [

            '/' => 'site/index',

            '<alias:login|logout|about|contact>' => 'site/<alias>'

        ]

    ],

    'urlManagerBackend' => [

        'class' => 'yii\web\urlManager',

        'baseUrl' => 'admin[dot]photography[dot]dev',

        'enablePrettyUrl' => true,

        'showScriptName' => false,

    ],*/

    'user' => [

        'identityClass' => 'common\models\User',

        'enableAutoLogin' => true,

        'enableSession' => true,

        'identityCookie' => [

            'name' => '_identity',

        ]

    ],

    'session' => [

        'name' => 'PHPFRONTENDBACKENDSESSID',

    ],

    'log' => [

        'traceLevel' => YII_DEBUG ? 3 : 0,

        'targets' => [

            [

                'class' => 'yii\log\FileTarget',

                'levels' => ['error', 'warning'],

            ],

        ],

    ],

    'errorHandler' => [

        'errorAction' => 'site/error',

    ],

What you will find when you get into deploying your app in production mode (live on the web) your back end will be on a sub-domain. Because the cookies will not be the same between domains you will need to set up your own method of authentication when moving between the two different domains (yes, a sub-domain IS a different domain!).

Nice little project! :slight_smile: