下面由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如何使用中间件实现禁止未登录用户访问页面的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号