Home  >  Article  >  Backend Development  >  Use of laravel middleware

Use of laravel middleware

不言
不言Original
2018-07-05 15:13:371882browse

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
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,    ];
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);
    }
}
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;);});

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!

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