Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create laravelHTTP Middleware

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report