Get the App
SLTechnology News&Howtos  ›  Development  › 

How to create laravelHTTP Middleware

Shulou Source: shulou.com Published: 2022-06-03 14:35:03 09月27日 Update

This article shows you how to create laravel HTTP middleware, the content is concise and easy to understand, can definitely make your eyes bright, through the detailed introduction of this article, I hope you can get something.

In fact, middleware adds a layer of filtering or protection to the routing. Prefixes and namespaces are passed as array parameters of group, and middleware is also passed as array parameters of group.

I. Middleware writing

Before adding middleware

Route::group (['prefix' = >' admin','namespace'= > 'Admin'], function () {Rount::get (' login','IndexController@login'); Rount::get ('index','IndexController@index');})

After adding web middleware

Route::group (['prefix' = >' admin','namespace'= > 'Admin','middleware'= > [' web']], function () {Rount::get ('login','IndexController@login'); Rount::get (' index','IndexController@index');})

Web middleware can use session function to enable CSRF protection

Route::get ('/', function () {session (['key'= > 123]); return view (' welcome');})

Set up another route to output session

Route::get ('/ test',function () {echo session ('key'); return' test';})

But because it is not in a middleware, the value of session cannot be taken out.

Let's set up the middleware that puts routing into a packet.

Route::group (['middleware'= > [' web']], function () {Route::get ('/', function () {session (['key'= > 123]); return view (' welcome');}); Route::get ('/ test',function () {echo session ('key'); return' test';});})

System default web middleware, middleware in Kernel.php

Session is used only when using web middleware

2. Manually define a middleware to manage cloud login in the Kernel.php file.

Before definition:

Protected $routeMiddleware = ['auth' = >\ Illuminate\ Auth\ Middleware\ Authenticate::class,' auth.basic' = >\ Illuminate\ Auth\ Middleware\ AuthenticateWithBasicAuth::class, 'guest' = >\ App\ Http\ Middleware\ RedirectIfAuthenticated::class,' throttle' = >\ Illuminate\ Routing\ Middleware\ ThrottleRequests::class,]

After definition:

Protected $routeMiddleware = ['auth' = >\ Illuminate\ Auth\ Middleware\ Authenticate::class,' auth.basic' = >\ Illuminate\ Auth\ Middleware\ AuthenticateWithBasicAuth::class, 'guest' = >\ App\ Http\ Middleware\ RedirectIfAuthenticated::class,' throttle' = >\ Illuminate\ Routing\ Middleware\ ThrottleRequests::class, 'admin.login' = >\ App\ Http\ Middleware\ AdminLogin::class,]

To change to the project directory in the cmd window, you can create middleware with the command

Php artisan make:middleware AdminLogin

Then view the AdminLogin.php file in the Middleware folder

Tags: Middleware login pages files information management success background folders administrators routing output content parameters that is skills arrays knowledge protection authentication Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Huawei Docker Shulou Technology Redmi