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中文網其它相關文章!