Use of laravel middleware

不言
Release: 2023-04-02 14:48:02
Original
1878 people have browsed it

This article mainly introduces the use of laravel middleware, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

Usage of laravel middleware:

Create middleware command
php artisan make:middleware CheckLogin
Copy after login
After executing the above command, a new middleware class CheckLogin.php will be created in the app/Http/Middleware directory.
After creation, you still need to register the middleware in app/Http/Kernel.php:
 protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        //这就是新注册的中间件
        'checklogin' => \App\Http\Middleware\CheckLogin::class,    ];
Copy after login
You can write verification in the newly created middleware as follows:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Session;
class CheckLogin{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $userid = Session::get(&#39;_userid&#39;);        
        $login_sts = Session::get(&#39;_login_sts&#39;);        
        if (empty($userid) || empty($login_sts)){            
        return response()->view(&#39;admin/login&#39;);
        }        
        return $next($request);
    }
}
Copy after login
The next step is how to use the middleware function
Route::group([&#39;namespace&#39;=>&#39;Admin&#39;,&#39;middleware&#39;=>&#39;checklogin&#39;],function (){    
Route::get(&#39;admins&#39;,&#39;IndexController@index&#39;);    
Route::get(&#39;logout&#39;,&#39;IndexController@logout&#39;);});
Copy after login

The routing group is used directly here. As long as the routing is placed in the group, it will go through this verification. ['namespace'=>'Admin'] is Namespace, ['middleware'=>'checklogin'] This is middleware verification. The registration name was checklogin when registering before, so just write checklogin directly after middleware.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Laravel Modify the default log file name and location

Use laravel dingo api plug-in library to create api method

The above is the detailed content of Use of laravel middleware. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!