Yii2 How to realize the separation before and after the station?

Source: Internet
Author: User
Tags yii

This article and everyone to share is mainly PHP in the Yii2 framework before and after the separation of knowledge, come together to see it, hope to help you.

Yii2 both front and back stations are required to register the time will be generated before and after the login one another synchronization login and exit, this is because the login and exit before the Sitecontroller inside the public Common/model The following loginform.php and user.php need to be written separately:

1, the common/models/user.php in the current directory copy a copy, named admin.php, Modify the name of the class to Admin

2. copy common/models/loginform.php to backend/models/loginform.php

Modify backend/models/loginform.php

namespace backend\models;

Use common\models\Admin;

...

...

protected function GetUser ()

{

if ($this->_user = = = null) {

$this->_user = admin::findbyusername ($this->username);

}

return $this->_user;

}

3. copy common/models/loginform.php to frontend/models/loginform.php

Modify frontend/models/loginform.php

namespace frontend\models;

Use common\models\User;

4. Delete common/models/loginform.php

5, modify the backend/config/main.php

' Components ' = [

' User ' = [

' Identityclass ' = ' common\models\admin ',

' Enableautologin ' = true,

],

...

]

6, modify the frontend/config/main.php

' Components ' = [

' User ' = [

' Identityclass ' = ' common\models\user ',

' Enableautologin ' = true,

],

...

]

7, modify the controller backend/controllers/sitecontroller.php

namespace backend\controllers;

Use Yii;

Use yii\Web\Controller;

Use yii\filters\verbfilter;

Use yii\filters\AccessControl;

Use backend\models\loginform;

If not, pay attention to the front and rear sync adjustments

front and back-end public a session , different users log in and out (foreground login user, background refresh automatically login to the corresponding administrator), this is a common session

in the main.php below the backend config

\ ' User\ ' = [

\ ' identityclass\ ' = \ ' common\models\admin\ ',

\ ' enableautologin\ ' = true,

\ ' identitycookie\ ' = = [\ ' name\ ' + \ ' __manage_identity\ ', \ ' httponly\ ' = ' = true],

\ ' idparam\ ' = \ ' __manage\ ',

Frontend in the same vein .

localhost:90/login/index 's background interface shows no permissions

in the background logincontroller , add

Use Backend\models\loginform;

can be displayed normally

Source: Blog Park

Yii2 How to realize the separation before and after the station?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.