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?