search
HomeBackend DevelopmentPHP TutorialLaravel 一步步实现权限控制(2) 登录重写

laravel 登录重写

权限控制是几乎每套成熟系统不可缺少的一部分,我们使用的权限控制方法是rbac,我将在这个系列的文章一步步完成一个比较复杂的rbac权限控制。

rbac权限控制是一个非常成熟的权限控制系统,其原理是给每个用户一个或多个角色 而每个角色对系统相应模块有访问权限,具体理论知识不多介绍。在我完成这个权限控制系统中,我将普通用户和管理员用户分开在数据库中存储,我们先完成普通用户的登录,这一部分相对于管理员用户会比较简单,同时也让大家理解下登录的流程。

未登录用户跳转到登录页面

当我们访问某些一定要登录后才能使用的功能时 我们往往会有一个功能就是 如果没有登录的用户,就会直接跳转到登录功能 在laravel已经提供的代码中我们可以非常轻松完成这个功能

php<br />class HomeController extends Controller {    public function __construct()    {        $this->middleware('auth');    }    public function index()    {        echo url('/home');        echo '这是首页';    }}

我们在随便一个需要进行登录才能使用的控制器中使调用auth这个控制器就可以了,这个控制器会去检查session中是否有登录信息来进行判断是否有没有登录 那么这个'auth'中间件在哪呢?

laravel中所有中间件都在app/Http/Kernel中注册

php    protected $routeMiddleware = [        'auth' => 'App\Http\Middleware\Authenticate',        'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',        'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',        'admin_auth' => 'App\Http\Middleware\AdminPermissionCheck',    ];

这个数组中key是中间件的别名 value是中间件的路径 由此我们可以找到别名为auth的中间件

找到App\Http\Middleware\Authenticate

php    public function handle($request, Closure $next)    {        if ($this->auth->guest())        {            if ($request->ajax())            {                return response('Unauthorized.', 401);            }            else            {                //这个方法会跳转到Auth控制器的getLogin方法                 //如果没有 那么会自动跳转的视图文件夹下的auth login                return redirect()->guest('auth/login');            }        }        return $next($request);    }

handle方法是调用中间件时调用的方法 其中guest是判断有没有登录的方法 这里我们最有可能需要改的地方就是如果没有登录跳转的方法 如上面代码所示 跳转的路径为auth/login(这个路径已经在路由中配好,跳转到Auth控制器中得getLogin方法)

建立一个登录视图

在AuthController中建立一个登录视图

php    public function getLogin(){        return view("auth.login");  }
表单数据验证

表单数据验证在实现部分是postLogin方法中的 UserLoginRequest $req
我们建立一个请求类来对表单进行数据验证 使用laravel 提供的php artsian make:request 能非常轻松的建立一个请求类

php<br />class UserLoginRequest extends Request {    /**     * Determine if the user is authorized to make this request.     *     * @return bool     */    public function authorize()    {        return true;    }    /**     * Get the validation rules that apply to the request.     *     * @return array     */    public function rules()    {        return [            //再这里对表单提交字段进行过滤            'identity' => 'required|min:3|max:16',            'password' => 'required|min:6|max:16'        ];    }    public function sanitize()    {        return $this->all();    }}
登录处理方法
php    public function postLogin(UserLoginRequest $req){   //这里对传递过来得字段进行了处理 这个函数为我自己定义的函数 仅仅是为了演示用         $identity = $this->generateLoginIdentity($req->input());         $identity['password'] = $req->input('password');        //验证用户账号密码        if($this->auth->attempt($indentity)){            //登录成功 记录用户登录时间和登录ip            $user = User::where('id','=',$this->auth->user()->id)->first();            // 触发一个事件            event(new \App\Events\UserLogin($user,$req->ip()));            //重定向到想要访问的页面            return redirect()->intended('/');        }    }

如上述代码所示 你可以对传过来得数据根据业务需要做进一步的处理

验证登录用户功能的代码是这一段$this->auth->attempt($indentity) 如果验证成功回返回true,这个函数是laravel自带Auth的一个方法 功能是去User表中匹配传过来的字段,如果需要验证更多字段,当验证成功后会将登录信息存入session中。当然Auth去查找匹配的表是可以更改的,我会在后面实现管理员用户的登录功能的时候演示怎么修改。

然后可以在下面的代码中继续完成你的业务逻辑 如我代码中所示的触发一个事件来记录登录事件和登录ip

已经登录 访问登录页面的自动跳转

如果已经登录了 再访问登录页面 显然我们不需要再出现登录视图让其登录,我们需要将其跳转到其他路径,这个路径依然是可以修改的

首先我们看看再Auth控制器中哪里对是否已经登录进行判断

php    public function __construct(Guard $auth, Registrar $registrar)    {        $this->auth = $auth;        $this->registrar = $registrar;        $this->middleware('guest', ['except' => 'getLogout']);    }

其中这个except表示getLogout这个方法将不会受到这个中间件的影响 getLogout通常是登出方法

在这个控制器的构造方法调用了一个中间件来对是否已经登录进行判断 ,通过查找kernel.php我们找到这个中间件
是App\Http\Middleware\RedirectIfAuthenticated.php

php    public function handle($request, Closure $next)    {        if ($this->auth->check())        {                     //跳转的路径            return new RedirectResponse(url('/'));        }        return $next($request);    }

只要修改了上面代码中跳转的路径就可以了

总结

我们可以看到 laravel已经给我们封装了很多登录相关需要用到的功能 ,非常的完善,但也给了我们很大的自由随意去修改登录相关的流程,我们可以随意根据需要业务逻辑修改登录的功能。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.