YII2 build the perfect backstage and implement RBAC privilege control case tutorial, Yii2rbac
Author: White Wolf Source: www.manks.top/article/yii2_frame_rbac_template
The copyright belongs to the author, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
1, installation Yii2
Not installed please refer to the YII2 history of the simplest installation tutorial, no one
Installed please continue to see the next steps
2. Configuration database
2.1 Configuration Database
Modify common/config/main-local.php The local database in the actual project is often inconsistent with the online database.
We configure here to main-local.php, after the product is online, we can use git or svn ignore main-local.php, direct deployment online.
The MySQL database we use here is configured as follows
Of course, the information above the red circle needs to be manually modified by you, if it is a coincidence with me, then there is no need to modify the
2.2 Create User Data table, we want to implement background login
Description: The user table and the menu table can be created by referring to the SQL in the component yii2-admin that we downloaded later, the specific directory is located in
Vendor\mdmsoft\yii2-admin\migrations\schema-mysql.sql
CREATE TABLE ' user ' (' id ' int (one) ' NOT NULL auto_increment COMMENT ' self-increment id ', ' username ' varchar (255) NOT null COMMENT ' user Name ', ' auth_key ' varchar (+) NOT null COMMENT ' Auto login key ', ' password_hash ' varchar (255) NOT null COMMENT ' encrypted password ', ' password_reset_token ' varchar (255) DEFAULT NULL COMMENT ' Reset Password token ', ' email ' varchar (255) NOT null COMMENT ' Mailbox ', ' role ' smallint (6) NOT null default ' COMMENT ' role rank ', ' status ' smallint (6) NOT null default ' COMMENT ' State ', ' created_at ' int (one) not null COMMENT ' creation time ', ' updated_at ' int (one) NOT null COMMENT ' update Time ', PRIMARY KEY ( ' id ') engine=innodb auto_increment=0 DEFAULT charset=utf8 comment= ' user table ';
2.3 Visit frontend site, register a user first
After successful registration, the top right corner will show the status of the login, we will use the registered users later
Next we'll start configuring the background template.
3. Render the background template with Adminlte
Background templates We use Adminlte (backend theme for Yii2 Framework)
Slot: Adminlte is a fully responsive administrative template. Easy-to-customize templates based on BOOTSTRAP3 framework. Suitable for a wide range of screen resolutions, from small mobile devices to large desktops.
Several pages are built-in, including dashboards, mailboxes, calendars, lock screens, login and registration, 404 errors, 500 errors, and more.
3.1 Installing Adminlte
Https://github.com/dmstr/yii2-adminlte-asset
Open the link above and follow the procedure to install it
Here I briefly outline my own installation steps, after CD advanced,
Because it is a Mac, here directly composer to install on the line composer require dmstr/yii2-adminlte-asset "2.*"
After successful installation, there will be several folders under the vendor directory, as follows
3.2 YII2 Configuration Integration Adminlte, build handsome on the background of the grade
Below we configure under backend/config/main.php first preview the effect, small heart catch nasty bar not hurry to taste the results
' components ' + = [' view ' = ' theme ' + [' pathmap ' = ' + ' @app/views ' and ' + ' @vendor/dmstr/ Yii2-adminlte-asset/example-views/yiisoft/yii2-app ' ], [],],
Nice, the page looks much more instantly.
The problem is, we are in the components of a separate configuration of the theme, so that later to modify the layout file what is very inconvenient to drop, below we copy the file down to cover the Yii's own layout.
Copy the layouts and site from the Vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app directory
Overwrite lauouts and site corresponding files in backend/views/directory
After overwriting, remember to block out the configuration items of the following view
To this, the background template construction is over, si not si very happy
4, below we use yii RBAC to achieve the background control of permissions
4.1 We first configure the next short route
The compontents of the backend/config/main.php file plus the following configuration
' Urlmanager ' = [ //used to indicate whether Urlmanager is enabled for URL beautification, known as the path format URL in Yii1.1, or renamed in Yii2.0. //not enabled by default. But in the actual use, especially the product environment, generally will be enabled. ' Enableprettyurl ' + true,// whether strict parsing is enabled, such as enabling strict parsing, requires that the current request should match at least 1 routing rules, //Otherwise it is considered an invalid route. //This option is only valid if Enableprettyurl is enabled. ' enablestrictparsing ' = False, //Whether the portal script is displayed in the URL. is a further complement to the beautification function. ' showscriptname ' = False, //Specifies a suffix that is appended to the URL, such as. html. Valid only when Enableprettyurl is enabled. ' suffix ' = ', ' rules ' and ' = '
/
' =
'/ View ", "
/"="
/" ],",
Next, create the. htaccess file below the root of the project Backend/web, and add the following, this file you can not create, it seems that really need to hone the training OH
Options +followsymlinksindexignore */*rewriteengine on# If a directory or a file exists, use it Directlyrewritecond
%{request_filename}!-frewritecond%{request_filename}!-d# otherwise forward it to index.phprewriterule. index.php
Of course, your Apache has to turn on the rewrite module.
Below we visit under the GII module test
Http://localhost/advanced/backend/web/gii
The checksum is possible.
4.2 Creating the data tables required for permission control
Of course, these yii2 are ready for us.
Open a. vendor/yiisoft/yii2/rbac/migrations/schema-mysql.sql file, create a data table in sequence
' Auth_assignment '; ' auth_item_child '; ' Auth_item '; ' auth_rule '; Additional menus menu table, you need to create a description: User table and Menu table creation can refer to Vendor\ Mdmsoft\yii2-admin\migrations\schema-mysql.sqlcreate TABLE ' menu ' ( ' id ' int (one) not NULL auto_increment, ' Name ' varchar ' is not NULL, ' parent ' int (one) default null, ' route ' varchar () default NULL, ' order ' int ( One) DEFAULT NULL, ' data ' text, PRIMARY key (' id '), key ' parent ' (' parent '), CONSTRAINT ' Menu_ibfk_1 ' FOREIGN KEY (' parent ') REFERENCES ' menu ' (' id ') on DELETE SET NULL on UPDATE CASCADE) Engine=innodb DEFAULT Charset=utf8
4.3 Download and install Yii2-admin
Reference Https://github.com/mdmsoft/yii2-admin
Follow the steps step by step installation, with Adminlte installation
We'll see the Mdmsoft directory below the vendor directory.
4.4 Permissions Configuration
Open backend/config/main.php Modify Configuration
' Modules ' = [' admin ' = ' = ' class ' = ' Mdm\admin\module ',], ' aliases ' and ' = ' @mdm/admin ' = ' @vendor/mdmsoft/yii2-admin ',],//here must add AuthManager configuration items ' components ' and [ ... The components array is joined with the AuthManager component, there are Phpmanager and Dbmanager two ways, //phpmanager the permission relationship is saved in the file, here is used Dbmanager way, Save the permission relationship in the database. ' AuthManager ' = = [ ' class ' = ' Yii\rbac\dbmanager ', ' defaultroles ' = [' guest '], ' , ...],
4.5 Our access under the Permissions module check how the effect
Http://localhost/advanced/backend/web/admin/route
Well, the interface is some, the following we speed up the pace of acceptance under our authority this piece in the end or not?
In general, this step is OK. The back can feel the ability to add routes to assign permissions.
Below we add the permission column on the left menu, the code can be copied directly, placed in
Within
- Permission Control
- Administrator
- Background users
- Permissions
- Routing
- Permissions
- Role
- Distribution
- Menu
Let's look at it, so that we can operate on the privilege very conveniently.
In this way, our access control is basically over, and the permissions need to be explained:
You should add a route first, then add a permission name, and then assign individual permissions to the role or individual.
5, how to use the menu to control menus?
In 4.5 steps we directly write UL Li's way to the left side of the menu to operate, it is also possible to achieve through the menu to control the permission to drop. But, as a non-convenient operation, and to increase the modification of what things have to need us to modify the program implementation, this is also TTM inconvenient. Fortunately, we have a wonderful family, DDV kill kill.
Well, it's a long way off. Think carefully, is not the 123456 tables we created and a menu table is not used to the Nan? How do you use the goods? Come on, let's get back to the chase.
Preferred, we visit/admin/menu/index add a few first-class menu, let's call first Level 1, Level 2, Level 3 bar, oh yes, forget to add the route, how to do it, we visit the/admin/route/index to move the left side of the route to the right, Otherwise, creating a new menu above will fail. When creating a menu, [map] [data] we're not filling it out.
After the addition, we open the layout file Left.php,use two class files, respectively, Yii\bootstrap\nav and Mdm\admin\components\menuhelper;
The operation of getting permission Menuhelper::getassignedmenu all done for us.
OK, let's delete the 4.5 added Sidebar-menu menu and add the following code to try it out
Echo Nav::widget ( [ ' encodelabels ' + = False, ' options ' = [' class ' = ' Sidebar-menu '], ' Items ' = = Menuhelper::getassignedmenu (Yii:: $app->user->id), ]);
Now that the Menu control permission we are basically OK, now you can continue to add the menu to try the effect.
To this, our background and RBAC control can be said to be very perfect, if you are in the process of trying to encounter any problems, the following message is good, we share the discussion.
http://www.bkjia.com/PHPjc/1121394.html www.bkjia.com true http://www.bkjia.com/PHPjc/1121394.html techarticle Yii2 set up the perfect backstage and realize RBAC permission control case tutorial, Yii2rbac Author: White Wolf Source: www.manks.top/article/yii2_frame_rbac_template This article copyright belongs to the author, welcome reprint, ...