laravel如何使用中间件实现禁止未登录用户访问页面
时间:2022-03-03 11:47
下面由Laravel教程栏目给大家介绍laravel使用中间件实现禁止未登录用户访问页面的方法,希望对需要的朋友有所帮助!
1、生成中间件
[root@localhost MRedis]# php artisan make:middleware CheckLogin Middleware created successfully.
2、实现中间件,在app\http\middleware\CheckLogin.php
public function handle($request, Closure $next) { if (!session('user')) { return redirect('login'); } return $next($request); }
3、注册中间件,在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, 'check.login' => \App\Http\Middleware\CheckLogin::class, // 这一行 ];
4、使用中间件(一定要把登录路由的放在外面)
Route::group(['middleware' => 'check.login'], function() {内部为,不想让未登录用户进的路由}
5、成功
以上就是laravel如何使用中间件实现禁止未登录用户访问页面的详细内容,更多请关注www.gxlsystem.com其它相关文章!