Home >PHP Framework >Laravel >How does laravel verify routing login access through middleware
The following tutorial column of Laravel will introduce laravel to login and access through middleware verification routing. I hope it will be helpful to friends in need!
##What does middleware do
Middleware provides a convenient mechanism to filter HTTP requests entering the application.For example, Laravel includes a middleware that authenticates users. If the user fails to authenticate, the middleware will redirect the user to the login page. On the other hand, if the user is authenticated, the middleware will further forward the request to the application.
Of course, in addition to identity verification, other middleware can also be written to perform various tasks. For example: CORS middleware can be responsible for adding appropriate response headers to all responses returned by the application. Logging middleware can log all requests coming into your application.
Laravel comes with some middleware, including authentication, CSRF protection, etc. All these middleware are located in the app/Http/Middleware directory.
Create middleware
//使用 make:middleware 命令来创建新的中间件。php artisan make:middleware Auth
Register middleware
Global middlewareIf you want the middleware to be used in the application Runs during processing of each HTTP request. Just list the middleware in the $middleware attribute in app/Http/Kernel.php.
Assign middleware to routes
Assuming that you want to assign middleware to a specified route, you should first assign a key to the middleware in the app/Http/Kernel.php file. By default, Laravel's built-in middleware is included under the $routeMiddleware attribute in this class. To add a custom middleware, just append it to the list and assign it a custom key. For example:
// 在 App\Http\Kernel 类中...protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,];
Writing of middleware
public function handle($request, Closure $next) { //通过判断session是否有用户名,判断是否登陆 if(!session('user')){ return redirect('/'); } return $next($request); }
Using middleware in routing
//namespace是命名空间,也就是你的controller文件在哪个文件夹里//middleware对应的就是你想调用的中间件,这里调用的是登录验证的中间件//auth就是你在App\Http\Kernel类中注册的名字Route::group(['namespace' => 'Admin', 'middleware' => ['auth']], function () { //后台首页 Route::get('admin','admincontroller@index')->name('admin'); //清除缓存路由 Route::get('admin/clear','admincontroller@clear')->name('clear'); //栏目资源路由 Route::resource('admin/classify','classifycontroller'); //公告资源路由 Route::resource('admin/notice','classifynoticecontroller');});In this way we You can easily determine whether the user is logged in in the background, and automatically jump to the login page if not logged in!
The above is the detailed content of How does laravel verify routing login access through middleware. For more information, please follow other related articles on the PHP Chinese website!