The following tutorial column of thinkphp framework will introduce to you the auth permission setting and implementation in Thinkphp5. I hope it will be helpful to friends in need!
1. Download the auth class and place it in the directory: extend\auth\auth.php
2. Execute the SQL statement in the class , you can create 3 tables in the database auth_group (user group table) auth_rule (authority rule table) auth_group_access (user and user group association table)
3. I want to add a level relationship to the rules (similar to unlimited Extreme classification) Add 3 fields to auth_rule (authority rule table) pid (parent id, 0 is top level authority) level (level) sort (sort), as shown below
4, first create the administrator table yourself, such as the admin table, add, delete, modify and check to design it normally. The group field is the user group to which it belongs
5.auth_group You can also make your own additions, deletions, modifications, id, user group name, status (on or off), rules (corresponding to the id of the rule table)
6 .auth_rule rule table additions, deletions, modifications and checks can also be made by yourself, id, name (controller/method), title (rule name) status (on or off),
7. When adding a user, select the user group to which the user belongs, uid (corresponding to the user id) group_id (corresponding to the id of the user group to which the user belongs), so that they are associated
8. Complete the member login function, set session('id') after successful login, and store the current logged-in member ID in the session
9. The most important thing The first step is to use the auth class for verification, and use
<?php namespace app\admin\controller; use think\Controller; use think\Request; use auth\Auth; //引入suth类 class Common extends Controller { public function _initialize(){ //初始化判断用户是否已经登陆 if(!session('uname')){ $this->error('请先登陆系统!','login/index'); } //获得当前页面的控制器 / 方法 $request=Request::instance(); $moudle=$request->module(); //获取当前控制器名称 $con=$request->controller(); //获取当前控制器名称 $action=$request->action(); //获取当前方法名称 $this->assign(array( 'con'=>$con, 'action'=>$action, )); $rules=$con.'/'.$action; //组合 控制器/方法 $auth=new Auth(); //实例化auth类 $notCheck=array('Index/index'); //都可以访问的页面 if(session('uid')!=1){ //不是超级管理员才进行权限判断 if(!in_array($rules,$notCheck)){ // 是否在开放权限里面 if(!$auth->check($rules,session('uid'))){ // 第一个参数 控制/方法 第二个参数:当前登陆会员的id $this->error('没有权限','index/index'); }; } } }
in the public page common.php for final display;
The above is the detailed content of Auth permission setting and implementation in Thinkphp5. For more information, please follow other related articles on the PHP Chinese website!

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools