首先自然是检查RbacController.php有没有问题啦。
但是修改了还是不行……
$ ./yii rbac/init
Exception 'yii\db\IntegrityException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'createPost' for key 'PRIMARY'
The SQL being executed was: INSERT INTO `auth_item` (`name`, `type`, `description`, `rule_name`, `data`, `created_at`, `updated_at`) VALUES ('createPost', 2, '新增文章', NULL, NULL, 1563835151, 1563835151)'
in J:\weixiyii\myblog\adv2.0.8\blogdemo2\vendor\yiisoft\yii2\db\Schema.php:674
Error Info:
Array
(
[0] => 23000
[1] => 1062
[2] => Duplicate entry 'createPost' for key 'PRIMARY'
)
实在没办法。找不到答案。
进去数据库,看来看去看不懂。
最后还是看优快云和博客园讲解yii的rbac。
参考文章:https://blog.youkuaiyun.com/lsx392502599/article/details/78544280
yii框架中操作RBAC
基于角色的权限访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问),的有前景的代替受到广泛的控制。
这里我使用的是高级版的Yii框架,首先在common文件夹中config文件夹中的main-local.php中加入这段:
'authManager' => [
'class' => 'yii\rbac\DbManager',
'itemTable' => 'auth_item',
'assignmentTable' => 'auth_assignment',
'itemChildTable' => 'auth_item_child',
],
删除所有新增表,四张auth,一张migration。
重新 ./yii migrate --migrationPath=@yii/rbac/migrations.
结果:
Exception 'yii\base\InvalidCallException' with message 'Setting read-only property: yii\console\Application::authManager'
最后,看博客园文章:yii2搭建完美后台并实现rbac权限控制实例教程
https://www.cnblogs.com/grimm/p/5706112.html
打开backend/config/main.php修改配置,修改'authManager‘就可以了。
嗯……仔细看,我修改的是
common/config/main.php
但是有一个问题:魏曦的视频,http://www.weixistyle.com/yii2.php。
优酷打开,1.5倍速,视频里面的命名空间是:
namespace console\command;
我看魏曦的github代码是:
namespace console\controllers;
因为之前出了错,所以我一直都用后面的。
<?php
namespace console\controllers;
use Yii;
use yii\console\Controller;
class RbacController extends Controller
{
public function actionInit()
{
}
}
现在貌似没问题。