Home >Backend Development >PHP Tutorial >Use of laravel middleware
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
php artisan make:middleware CheckLogin
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, ];
<?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('_userid'); $login_sts = Session::get('_login_sts'); if (empty($userid) || empty($login_sts)){ return response()->view('admin/login'); } return $next($request); } }
Route::group(['namespace'=>'Admin','middleware'=>'checklogin'],function (){ Route::get('admins','IndexController@index'); Route::get('logout','IndexController@logout');});
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!