In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about what laravel middleware is commonly used to share with you. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Laravel commonly used middleware are: 1, "$middleware" global middleware, to do some processing on all requests, it is suitable to be defined in this component; 2, "$routeMiddleware" routing middleware; 3, "$middlewareGroups" middleware group.
The operating environment of this tutorial: windows7 system, Laravel6 version, Dell G3 computer.
Three middleware commonly used in laravel
$middleware attribute:
This attribute is called global middleware, so why is it global middleware? Because of every request you make, every middleware in it will be executed.
$routeMiddleware attribute:
This attribute is called routing middleware, so why is it called routing middleware? Because the middleware defined within this property can only be referenced when defining a route.
Suppose this is our definition of routing middleware:
Protected $routeMiddleware = [. 'auth' = >\ Illuminate\ Auth\ Middleware\ Authenticate::class,]
What should we do if we want to use the middleware\ Illuminate\ Auth\ Middleware\ Authenticate::class?
Route::get ('hello/laravel-china','XXController@index')-> middleware (' auth')
When we define a route, we call the middleware method with a parameter value of auth, so that when the route is accessed, the middleware is executed. You got it! It's very simple.
$middlewareGroups attribute:
This property is called a middleware group, so why is it a middleware group? We talked about routing middleware before. do you find it troublesome to add routing middleware like this? for example, if we implement 10 middleware, do we have to add 10 when defining routing? You don't have to go to so much trouble with the middleware group. Let's take a look at how middleware groups are defined.
Protected $middlewareGroups = ['web' = > [\ App\ Http\ Middleware\ EncryptCookies::class,...],' api' = > [...],]
Well, the above example is to define the format of the middleware group. For example, if we look at the key value of web, it corresponds to an array, which is composed of multiple middleware. When we have defined it, how should we use it?
When we declare the route, we can call it this way.
Route::group (['middleware' = >' web'], function ($route) {$route- > get ('hello/world',function () {}); $route- > get (' hello/php',function () {}); / / so that when accessing these routes, the middleware corresponding to the middleware group web will be executed, which is much more convenient and batch. });
Summary:
$middleware: global middleware, which is defined in this property when all requests need to be processed. (such as counting the number of requests)
MiddlewareGroups: middleware groups, such as when our project has api requests and web requests, we need to separate the two types of request middleware, so we need our middleware group.
RouteMiddleware: routing middleware, some individual requests, when we need to execute special middleware, it is suitable to be defined in this property.
Reference: https://laravel-china.org/topics/7392/laravel-three-middleware-functions
Thank you for reading! This is the end of this article on "what is the common middleware in laravel?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.