1.新建sys_user表此表后台管理用用户专用
bogon:my-cms yyc$ ./yii migrate/create Sys_user
Yii Migration Tool (based on Yii v2.0.24)
Create new migration '/data/www/my-cms/console/migrations/m190801_095645_Sys_user.php'? (yes|no) [no]:yes
New migration created successfully.
添加表结构(yii2默认已经建好了)
class m130524_201442_init extends Migration
{
public function up()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
$this->createTable('{{%user}}', [
'id' => $this->primaryKey(),
'username' => $this->string()->notNull()->unique(),
'auth_key' => $this->string(32)->notNull(),
'password_hash' => $this->string()->notNull(),
'password_reset_token' => $this->string()->unique(),
'email' => $this->string()->notNull()->unique(),
'status' => $this->smallInteger()->notNull()->defaultValue(10),
'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(),
], $tableOptions);
}
执行
./yii migrate
(1)数据库中插入一条管理员 admin 123456
INSERT INTO `sys_user` (`id`, `username`, `auth_key`, `password_hash`, `password_reset_token`, `email`, `status`, `created_at`, `updated_at`, `verification_token`) VALUES
(1, 'admin', 'ILogjjGdhLcUZRrY1P8AvWVbxA2dqlLg', '$2y$13$1W63oCkPaUdLwjdaDA3mROZ2tMBsRdAcdSFgtdOYIgJUHpuG.hyVK', NULL, '', 10, 1564709873, 1564709873, NULL);
(2)使用gii model生成SysUser 并使SysUser继承common/models/User
<?php
namespace backend\models\system;
use Yii;
class SysUser extends \common\models\User
{
public $password;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'sys_user';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['username', 'auth_key', 'password_hash', 'email', 'created_at', 'updated_at'], 'required'],
[['status', 'created_at', 'updated_at'], 'integer'],
[['username', 'password_hash', 'password_reset_token', 'email', 'verification_token'], 'string', 'max' => 255],
[['auth_key'], 'string', 'max' => 32],
[['username'], 'unique'],
[['email'], 'unique'],
[['password_reset_token'], 'unique'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'auth_key' => 'Auth Key',
'password_hash' => 'Password Hash',
'password_reset_token' => 'Password Reset Token',
'email' => 'Email',
'status' => 'Status',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'verification_token' => 'Verification Token',
];
}
(3)新建后台登陆的LoginForm 并使LoginForm继承common/models/LoginForm,并重写getUser方法(注意$_user为私有属性无法继承,getUser的user使用后台backend\models\system\SysUser类)
<?php
namespace backend\models\system;
/**
* Login form
*/
class LoginForm extends \common\models\LoginForm
{
private $_user;
/**
* Finds user by [[username]]
*
* @return User|null
*/
protected function getUser()
{
if ($this->_user === null) {
$this->_user = SysUser::findByUsername($this->username);
}
return $this->_user;
}
}
(4) 在backend/config/main.php 中配置identityClass 为backend\models\system\SysUser
'user' => [
'identityClass' => 'backend\models\system\SysUser',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
],
(5)修改site/login方法的LoginForm 修改为后台model
use backend\models\system\LoginForm;
然后尝试登陆一下,成功!后台登陆功能完成。