打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Yii2 RBAC

yii2搭建完美后台并实现rbac权限控制, refernce: http://www.manks.top/yii2_frame_rbac_template.html
1. 安装yii2 advanced
composer create-project --prefer-dist yiisoft/yii2-app-advanced advanced
cd advanced
php init
vim common/config/main-local.php    //配置数据库访问信息
'db' => [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=yii2advdev',
    'username' => 'root',
    'password' => '123456',
    'charset' => 'utf8',
],
./yii migrate   //创建user 表,前台注册登录

localhost/advanced/frontend/web
localhost/advanced/backend/web


2. 利用AdminLTE渲染后台模板
cd advanced
composer require dmstr/yii2-adminlte-asset "2.*"
拷贝 vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app 目录下的layouts和site目录以及文件
覆盖掉backend/views/目录下的lauouts和site对应的文件


3.完善rbac权限管理
3.1 配置简短路由
backend/config/main.php文件的compontents加上下面的配置 
"urlManager" => [    
    //用于表明urlManager是否启用URL美化功能,在Yii1.1中称为path格式URL,    
    // Yii2.0中改称美化。   
    // 默认不启用。但实际使用中,特别是产品环境,一般都会启用。   
    "enablePrettyUrl" => true,    
    // 是否启用严格解析,如启用严格解析,要求当前请求应至少匹配1个路由规则,    
    // 否则认为是无效路由。    
    // 这个选项仅在 enablePrettyUrl 启用后才有效。    
    "enableStrictParsing" => false,    
    // 是否在URL中显示入口脚本。是对美化功能的进一步补充。    
    "showScriptName" => false,    
    // 指定续接在URL后面的一个后缀,如 .html 之类的。仅在 enablePrettyUrl 启用时有效。    
    "suffix" => "",    
    "rules" => [        
        "<controller:\w+>/<id:\d+>"=>"<controller>/view",  
        "<controller:\w+>/<action:\w+>"=>"<controller>/<action>"    
    ],
],

3.2 配置apache web server
在应用的目录 backend/web下面创建.htaccess文件并添加如下内容
Options +FollowSymLinks
IndexIgnore  */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php


3.3 apache support rewrite
cd /etc/apache2
a2enmod rewrite
执行后会在mods-available目录下创建一个快捷方式,连接到mods-enabled下rewrite模块。

Rewrite库加载不代表我们的.htaccess文件就能生效,还需要专门打开指定目录的AllowOveride开关,apache2.conf(AllowOverride None 修改为: AllowOverride All), 或者sites-enabled/000-default, 将其中的 AllowOverride None 修改为: AllowOverride All
<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride all
        Require all granted
</Directory>

service apache2 restart



http://localhost/advanced/backend/web/gii
 gii模块仅限本地访问,默认得用127.0.0.1或者localhost地址. 
可以正常打开即没问题。


3.4 下载安装 yii2-admin
cd advanced
composer self-update
composer install --prefer-dist

composer update
composer require mdmsoft/yii2-admin "~2.0"
安装好了可以在 vendor 目录下看到 mdmsoft 目录 


3.5 权限配置
打开backend/config/main.php 修改配置 



我在安装的时候遇到问题,rbac迁移的时候报异常,在官网(http://www.yiiframework.com/doc-2.0/guide-security-authorization.html)找到这个:
Note: If you are using yii2-basic-app template, there is a config/console.php configuration file where the authManager needs to be declared additionally to config/web.php. In case of yii2-advanced-app the authManager should be declared only once in common/config/main.php.

把authManager配置项移到common/config/main.php就好了!!!!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
是时候使用 Vue2 和 Yii2 进行前后端分离开发啦
windows环境下wampserver的配置教程——超级详细
zend framework开发环境搭建-windows
编程语言相干phpmyadmin安装的疑问
winxp下安装APACHE,PHP,MYSQL,PHPMYADMIN教程 | Drupa...
PHP服务器配置(使用Apache及IIS两种方法)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服