这篇文章主要介绍了关于laravel中间件的使用,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
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');});
这里直接使用的是路由群组,只要把路由放在群组之中都会经过这个验证,[‘namespace’=>’Admin’]是命名空间,[‘middleware’=>’checklogin’]这就是中间件验证,之前注册时注册名为checklogin,所以middleware后直接写checklogin就可以了。
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
Atas ialah kandungan terperinci laravel中间件的使用. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!