Yii2 框架 - RBAC

195 阅读1分钟

此配置基于 yii2 基础版

  1. 在 config/console.php 以及 config/web.php 的 components 内添加 authManager
'components' => [  
	'authManager' => [  
		'class' => 'yii\rbac\DbManager'  
	],
  1. 打开控制台执行命令
yii migrate --migrationPath=@yii/rbac/migrations

此操作会生成 5 张表

  • auth_assignment
  • auth_item
  • auth_item_child
  • auth_rule
  • migrations
  1. 使用迁移生成用户表
yii migrate/create create_user_table

修改 migrations 目录下的 create_user_table.php 文件

public function safeUp() {  
	$tableOptions = null;  
	 if ($this->db->driverName === 'mysql') {  
 		$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 生成用户表 4. 修改 User 模型使其继承自 yii\db\ActiveRecord[用户表模型]