欢迎光临企业官网建站网站,提供自助建设系统平台服务

企业官网建站

专业为公司品牌推广建设网站

yii2-基础 2.搭建博客 - 配置数据迁移和后台登陆

作者:jcmp      发布时间:2021-05-08      浏览量:0
1.新建sys_user表此表后台管理用

1.新建sys_user表此表后台管理用用户专用

bogon:my-cms yyc$ ./yii migrate/create Sys_userYii 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]:yesNew migration created successfully.

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。

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', ]; }

_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;

然后尝试登陆一下,成功!后台登陆功能完成。