Yii2 RBAC Privilege Control Menu Instance Tutorial _php instance

Source: Internet
Author: User
Tags yii

In the article to introduce you to the YII2 to build a perfect background and the implementation of RBAC rights control examples of the perfect implementation of the yii2 of the background and RBAC rights control, if you have not achieved, please see the above and then back to the reference article, because this article is based on the above to improve and complement.

Some of the boys have reflected, the last menu how to control the permissions ah, do not understand, do not know, and you do not seem to have finished, blind cheat! Really not so full, today we see how to achieve the menu perfect permissions. The first list of the main content, do not need to look down, just to share to those in need.

Add menus using the menu table

Left menu Results Adminlte Perfect presentation

The menu is preceded by a custom icon small icon and renders

How to control the display or hiding of menus

Page action button does not have permission to do not show

Only buttons that have permission to operate are displayed in the GridView

Well, that's pretty much it, if you have other permissions to control the requirements of the menu, please leave a message below, we share exchange.

First look at the picture preview of the general effect, or how to arouse your studious heart.

First we first visit the routing page/dmin/route/index, as far as possible to the left of all selected, and then moved to the right, and then remember to the current user to assign the relevant access rights OH.

Next Access Menu list/admin/menu/index Click Add menu, if you do not have permission to access this time, please temporarily allow the current user to access any permissions in the as Access file of the profile, and remove the as access settings when we have added permissions. Be sure to remember oh, otherwise the authority is invalid, don't come back for me.

Here we first fill out the four fields of the winning red

For example: We want to add a level menu "Rights Management"

Name please fill in "Rights management", because it is a level menu, the parent name please leave blank, routing us here Select "/admin/default/index", note that if matching to any route, please visit the routing page to the left of the selection to the right.

mapping, in fact, is the ranking of the problem, such as I added two menus, mapping value of 1 on the above value of 2 on the bottom of 1, we fill in the number 1 here.

We'll talk about the "data" later.

Add it in the way just described, and give an example of adding level two menu "permission Control"

Just fill in the parent name here we just added a successful first-level name "Rights Management" can be. Other usual. will not operate more than two times, it is best to manage the rights of a few menu all add up, because next we need and you can practice practicing well.

We have added a menu to this.

Let's take a look at the next step, using the background framework we built earlier.

Use Mdm\admin\components\menuhelper; 
Echo dmstr\widgets\menu::widget ([
' Options ' => [' class ' => ' Sidebar-menu '], 
' Items ' => menuhelper:: Getassignedmenu (Yii:: $app->user->id), 
]);

Is it simple, What? I do not dmstr this thing, I am sorry, has said many times, need to see above and then there is the combination of this menu.

OK, refresh the page to see your menu, although we haven't fully implemented the functionality we want, but you should be able to see your menu display.

Next we will enter our focus as well as the difficulty, we want to implement the small icon in front of the menu and complete the menu to hide control.

Remember when we first created the menu, the only option "data" that was not filled in? In order to facilitate the subsequent operation, we are here together with the icon and hidden options are filled in. Let's start with the screenshot of the "Backend user management" of this two-level menu as an example to modify. Screenshots we first look at the current state of our menu,

Please fill in the following code in the "Data" item of the update page, if you have new idea, please follow my thought to make the change later, because we want to carry on the operation later.

{"icon": "FA fa-user", "visible": false}

Yes, this "data" item we fill out is a section of JSON code, next we have to show the menu of the code to do a little bit of rectification, in order to facilitate everyone to see the code, here $callback directly written on the page, back to you can continue to package optimization.

 use Mdm\admin\components\menuhelper; 
<?php $callback = function ($menu) {$data = Json_decode ($menu [' Data '], true); 
$items = $menu [' Children ']; 
$return = [' label ' => $menu [' name '], ' url ' => [$menu [' Route ']],]; 
Process our configuration if ($data) {//visible isset ($data [' visible ']) && $return [' visible '] = $data [' Visible ']; 
Icon Isset ($data [' Icon ']) && $data [' icon '] && $return [' icon '] = $data [' Icon ']; 
Other attribute e.g. class ... $return [' options '] = $data; 
}//Not Configured icon display default icon (!isset ($return [' icon ']) | |! $return [' icon ']) && $return [' icon '] = ' fa fa-circle-o '; 
$items && $return [' items '] = $items; 
return $return; 
};  Here we optimized the menu menus for the first write. Echo dmstr\widgets\menu::widget ([' Options ' => [' class ' => ' Sidebar-menu '], ' items ' => Menuhelper::getassignedmenu (Yii:: $app->user->id, NULL, $callback),]; 

Yes, you are not mistaken, the code is a bit long, we have to analyze the simple analysis. In fact, our $callback method is just the analysis of the "data" item above which we add the menu. It is not difficult to see that we have the icon and visible respectively to judge, and append to the current menu.

To this end, we have a few twists and turns to achieve the menu plus icon and set the menu hidden. If you have other properties that need to be configured, you can still continue to configure this individually.

Now, we still have two more tasks to finish, to complete the next two tasks, for the convenience of everyone, we recommend that your project can be customized configuration gii templates, do not support customizing the GII template can be referenced custom GII stencil when you find out what we need to do behind it, I think you'll be amazed at what we do here.

For ease of operation, we'll add a test table here, and we'll start with a model+curd in our unmodified GII stencil. We first assign all the test/* permissions to the currently logged-on user and add a single piece of data. Open/test We can normally see the screenshot below, note the two marks in the figure.

Then we open the test/index.php file and make the following modifications to the two places marked

Use Mdm\admin\components\helper; 
<?php 
//No Create permission does not show button 
if (Helper::checkroute (' create ')) { 
echo html::a (' Create Test ', [' Create '], [' Class ' => ' btn btn-success ']); 
} ?> 
<?= gridview::widget ([ 
//... 
] Columns ' => [/ 
/... 
] [ 
' class ' => ' Yii\grid\actioncolumn ', ' 
template ' => helper::filteractioncolumn (' {view}{update}{ Delete} '), [],],] 
;?>

Now we refresh the next page/test look, it seems that nothing changes. Don't worry, we now revoke the current user's/test/create permissions and/test/update permissions, and then you will refresh the page to see the effect, we take a screenshot to see.

That is, the button that we do not have permission disappears, and when you manually visit the/test/create you will notice that you are not authorized.

If your gii template is customized, I believe you will quickly add rights control to all the actions in the template that it generates, isn't it happy

Finally, our menu on the control of the permissions of the basic end of the speech, long speech, careful recall also not many things, but I hope to help you one or two

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.