Yii2前後台分離

來源:互聯網
上載者:User

標籤:註冊   mode   access   http   同步登入   登入   ram   enable   orm   

Yii2前後台都需要註冊的時候會產生前後台登入一個另一個同步登入和退出,這是因為登入和退出之前的sitecontroller裡面公用了common/model下面的LoginForm.php和user.php需要分開寫:

1、將common/models/User.php在目前的目錄下copy一份,命名為Admin.php,修改類的名稱為Admin

2、將common/models/LoginForm.php複製到backend/models/LoginForm.php

修改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、將common/models/LoginForm.php複製到frontend/models/LoginForm.php

修改frontend/models/LoginForm.php

namespace frontend\models;use common\models\User;

 

4、刪除common/models/LoginForm.php

 

5、修改backend/config/main.php

 

‘components‘ => [        ‘user‘ => [            ‘identityClass‘ => ‘common\models\Admin‘,            ‘enableAutoLogin‘ => true,        ],        ...]       

 

6、修改frontend/config/main.php

 

‘components‘ => [        ‘user‘ => [            ‘identityClass‘ => ‘common\models\User‘,            ‘enableAutoLogin‘ => true,        ],        ...]

 

7、修改控制器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;

如果不行注意前背景同步處理調整

 

前台和後台公用一個session了,不同使用者同登入退出(前台登入了使用者,後台重新整理自動登入對應的管理員了),這是公用session

在backend的config下面的main.php中

‘user‘ => [            ‘identityClass‘ => ‘common\models\Admin‘,            ‘enableAutoLogin‘ => true,            ‘identityCookie‘ => [‘name‘ => ‘__Manage_identity‘, ‘httpOnly‘ => true],            ‘idParam‘ => ‘__Manage‘,

 

同理加在frontend中

 

localhost:90/login/index的後台介面顯示沒有許可權

在後台loginController中加

use backend\models\LoginForm;

可以正常顯示

 

Yii2前後台分離

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.