Yii2搭建后台并实现rbac权限控制完整实例教程,yii2rbac_PHP教程
Yii2搭建后台并实现rbac权限控制完整实例教程,yii2rbac
1、安装yii2
未安装的请参考yii2史上最简单式安装教程,没有之一
已安装的请继续看下一步操作
2、配置数据库
2.1 配置数据库
修改common/config/main-local.php 实际项目中本地的数据库往往跟线上数据库不一致,
我们这里配置到main-local.php就可以了,产品上线后,我们可以使用git或者svn忽略掉main-local.php,线上直接部署。
我们这里使用的mysql数据库,配置如下
当然啦,上面红圈圈的信息需要你自己手动修改掉,要是十分巧合跟我的一样那就不用在修改了
2.2 创建user数据表,我们后面要实现后台登陆
说明:user表和menu表的创建可以参考我们后面下载的组件yii2-admin里面的sql,具体目录位于
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
2.3 访问frontend站点,先注册个用户
注册成功后,右上角会显示登陆的状态,我们后面会用到这个注册的用户
接下来我们要开始配置后台的模板了。
3、利用AdminLTE渲染后台模板
后台的模板我们采用利用 AdminLTE(Backend theme for Yii2 Framework)
插播一曲:AdminLTE是一个完全响应管理模板。基于Bootstrap3框架,易定制模板。适合多种屏幕分辨率,从小型移动设备到大型台式机。
内置了多个页面,包括仪表盘、邮箱、日历、锁屏、登录及注册、404错误、500错误等页面。
3.1 安装AdminLTE
https://github.com/dmstr/yii2-adminlte-asset
打开上面的链接,按照操作步骤进行安装
这里我简述下自己的安装步骤,cd advanced后,
因为是mac,这里直接composer进行安装就行了
composer require dmstr/yii2-adminlte-asset "2.*"
安装成功后,vendor目录下会多几个文件夹,如下
3.2 yii2配置整合AdminLTE,搭建帅气上档次的后台
下面我们配置下backend/config/main.php先预览下效果,小心脏捉急的巴不得赶紧尝尝战果
1 2 3 4 5 6 7 8 9 |
|
nice,页面瞬间好看多了。
问题来了,我们是在components里面单独配置的theme,这样以后要修改布局文件什么的是很不方便滴,下面我们把不局文件拷贝下来覆盖掉yii自带的layout.
把vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app目录下的layouts和site拷贝
覆盖掉backend/views/目录下的lauouts和site对应的文件
覆盖后记得屏蔽掉 components下面view的配置项
到此,后台模板的搭建告一段落,si不si很happy
4、下面我们用yii的rbac实现后台的权限控制
4.1 我们先配置下简短路由
backend/config/main.php文件的compontents加上下面的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
接下来在项目的根目录 backend/web下面创建.htaccess文件并添加如下内容,这个文件你都创建不下来,看来是真需要再磨练磨练哦
1 2 3 4 5 6 7 8 |
|
当然,你的apache必须要开启rewrite模块。
下面我们访问下gii模块测试下
http://localhost/advanced/backend/web/gii
校验是可以的。
4.2 创建权限控制所需要的数据表
当然,这些yii2都给我们准备好了。
打开 vendor/yiisoft/yii2/rbac/migrations/schema-mysql.sql 文件,依次创建数据表
1 2 3 4 |
|
另外补充菜单menu表,需要的自行创建
说明:user表和menu表的创建可以参考 vendor\mdmsoft\yii2-admin\migrations\schema-mysql.sql
1 2 3 4 5 6 7 8 9 10 11 |
|
4.3 下载安装 yii2-admin
参考 https://github.com/mdmsoft/yii2-admin
按照步骤一步一步安装即可,同adminlte的安装
安装好了我们会在vendor目录下面看到 mdmsoft目录
4.4 权限配置
打开backend/config/main.php修改配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
4.5 我们访问下权限模块检验下效果如何
http://localhost/advanced/backend/web/admin/route
嗯,界面是有的了,下面我们加快脚步验收下我们的权限这块到底成还是不成呢?
一般来说到这一步就ok的了。后面的可以自己摸索着添加路由分配权限了。
下面我们在左侧菜单上把权限的栏目加上,代码可直接复制,放置于
内1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
我们看下效果图,这样一来,我们对权限进行操作就十分的方便了
如此,我们的权限控制基本告一段落了,关于权限需要说明的是:
你应该先添加路由,然后添加权限名称,后再对角色或个人进行权限的独立分配。
5、如何利用menu对菜单进行控制?
4.5步骤中我们是直接写ul li的方式对左侧菜单进行操作的,这样也是能够实现通过菜单对权限进行控制滴。
但是喃,一来不方便操作,而来增加修改个什么东东都得需要我们去修改程序实现,这也TTM不方便了。还好我们家有妙招,DDV杀杀杀。
好了,又扯远了。仔细回想,是不是我们创建的123456张表还有一张menu表没有利用到喃?这货怎么利用喃?来,我们回归正题。
首选,我们访问/admin/menu/index添加几个一级菜单,姑且叫做一级1,一级2,一级3吧,哦对了,忘记添加路由了,怎么回事呢,我们先访问/admin/route/index 把左侧的路由移动到右侧,不然上面创建新菜单会失败哦。创建菜单时,[映射][数据]我们暂且不填写。
添加完毕之后呢,我们打开布局文件left.php,use两个类文件,分别是yii\bootstrap\Nav和mdm\admin\components\MenuHelper;
获取权限的操作 MenuHelper::getAssignedMenu都帮我们做好了。
ok,我们删掉4.5添加的sidebar-menu菜单,添加下面的代码试试看
1 2 3 4 5 6 7 |
|
现在菜单控制权限我们基本上是ok了,现在你可以继续添加菜单试试效果如何。
到此呢,我们的后台和rbac的权限控制可以说是做得非常完美了,如果你在尝试的过程中遇到任何问题,下面留言就好,咱们共同交流探讨。
相关阅读:
Yii2 rbac权限控制之菜单menu实例教程

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

ThinkPHP6 backend management system development: Implementing backend functions Introduction: With the continuous development of Internet technology and market demand, more and more enterprises and organizations need an efficient, safe, and flexible backend management system to manage business data and conduct operational management. This article will use the ThinkPHP6 framework to demonstrate through examples how to develop a simple but practical backend management system, including basic functions such as permission control, data addition, deletion, modification and query. Environment preparation Before starting, we need to install PHP, MySQL, Com
