YII2 RBAC Permissions Control Menu Example Tutorial, yii2rbac_php Tutorial

Source: Internet
Author: User
Tags yii

YII2 RBAC Permissions Control Menu Example Tutorial, Yii2rbac


In the previous article to introduce the YII2 set up the perfect backstage and implement RBAC rights control example of the perfect implementation of the YII2 background and RBAC permissions control, if you have not realized, please look at the above again to refer to this article, because this article is based on the above to improve and supplement.

Some of the boys have reflected, the last menu how to control the permissions ah, do not understand, do not, and you do not seem to finish, blind fool! Really not so full, today we see how to implement the menu of perfect authority. First listed under the main content, do not need to look down, just to share to the people in need.

Adding menus using the menu table

Left menu result Adminlte Perfect presentation

Customize icon Small icon in front of menu and render

How to control the display or hiding of menus

The page action button does not have permission to do the display

Only buttons with Operation permissions are displayed in the GridView

Well, almost so much, if you have other rights control menu requirements, please leave a message, we share the exchange.

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

First we go to the routing page/dmin/route/index, as far as possible to the left to select all, and then move to the right, and then remember to assign the current user the relevant accessible permissions Oh.

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

Here we first fill in the Red four fields

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

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

mapping, in fact, is the ranking of the problem, for example, I added two menu, the map value of 1 is above the value of 2 is 1 below, we fill in the number 1.

"Data" We'll talk about that later.

Add it in the way you just introduced it, with an example of adding a Level two menu "permission Control"

Just fill in the parent name here. We have just added a successful first-level name "Rights Management". Other usual. Do not operate to see more than two times, it is best to add a few rights Management menu to complete, because next we need and you can practice practiced hand more.

We have already added the menu to this.

Let's take a look at the next step and use the background frame we built earlier to show it.

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

Isn't it simple, what? I do not dmstr this thing, I am sorry, have said many times, need to look at the above and then there is a combination of this menu.

OK, refresh the page to see your menu, although we have not fully implemented the features we want, but you should be able to see your menu show.

The next step is to enter our focus and the difficulty, we want to implement the small icon in front of the menu and complete the menu can be hidden control.

Remember when we first created the menu, the only option that didn't fill out "data"? For easy follow-up, we'll fill in the icons and hidden options here. We've modified this two-level menu, "Back-end user management" in the beginning. Let's take a look at the current state of our menu,

Please be sure to fill in the following code in the "Data" item of the update page, if you have a new idea, please follow my thoughts and then make the changes, because we want to do the work later.

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

Yes, this "data" item we fill in a JSON code, and then we will show the code of the menu to do a bit of rectification, in order to facilitate everyone to see the code, here $callback directly on the page, back to everyone can continue packaging 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 '],]; Handle 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; }//Do not configure the icon to display the default icon (!isset ($return [' icon ']) | |! $return [' icon ']) && $return [' icon '] = ' fa fa-circle-o '; $items && $return [' items '] = $items; return $return; }; Here we have optimized the menu of the first writing of the 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 and we come to simple analysis and analysis. In fact, the $callback method here is only for the analysis of the "data" item that we added to the menu above. It is not difficult to see that we judge the icon and the visible separately, and append it to the current menu.

Here, we took a few twists and turns finally realized the menu plus icon and the settings menu hidden. If you have additional properties that need to be configured, you can still continue to configure them separately here.

Now, we have two more tasks to complete, to complete the next two tasks, for the convenience of everyone, it is recommended that your project can be customized configuration of the GII template, do not support the customization of the GII templates can be referenced when you find the function that we need to do, I think you will marvel at our operations here.

To facilitate the operation, we have added a test table, testing, and we first generated a copy of the Model+curd with the non-modified GII template. We first assign all of the test/* permissions to the currently logged on user and add a piece of data. Open/test We can see the following, notice the two markers in the figure.

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

Use Mdm\admin\components\helper; <?php//Do not 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, there seems to be no change. Don't worry, we now revoke the current user's/test/create permissions and/test/update permissions, and then you refresh the next page to see the effect, let's see.

That is to say, we do not have access to the button disappears, when you manually access the/test/create you will find that will also remind you that you do not have permission.

If your gii template is customized, trust that you will quickly add permissions control on all the actions in its generated template, is not very happy

Finally, our menu on the control of the right to the basic explanation of the end, long-winded, carefully recalled that there are not many things, it is hoped to help you one or two

http://www.bkjia.com/PHPjc/1123823.html www.bkjia.com true http://www.bkjia.com/PHPjc/1123823.html techarticle Yii2 RBAC Permissions Control Menu Example Tutorial, Yii2rbac in the previous article to introduce the YII2 set up the perfect backstage and the implementation of RBAC rights control example of the perfect implementation of the YII2 backstage ...

  • 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.