In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today Xiaobian to share with you how laravel middleware custom related knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for everyone to refer to, I hope you read this article after some gains, let's learn about it together.
What is middleware?
Middleware provides a convenient mechanism for filtering HTTP requests into applications. For example, Laravel has a middleware built in to verify that the user is authenticated (e.g., logged in); if the user is not authenticated, the middleware redirects the user to the login page; if the user is authenticated, the middleware allows the request to proceed to the next step. In addition to authentication functions, middleware can be used to handle many other tasks. For example, CORS middleware can add appropriate headers (cross-domain) for outgoing sites; logging middleware can log all incoming requests to make it easier to build system logging systems.
Lavarel comes with some middleware, including certification, CSRF protection middleware and so on. All middleware is located in the app\Http\Middleware directory.
I. Custom middleware
Steps:
Create middleware using the artisan command php artisan make:middleware middleware name
Writing logic in created middleware
Register middleware in the Kernel.php configuration file in the app\Http folder
Assign middleware to routes or controllers
II. An example
For example, some amusement park projects need to be 12 years old or taller than 1 meter 4 can play.
We create a middleware for checking age php artisan make:middleware CheckAge
Closure is a closure function
$request An array of requests to receive applications
$next Pass the request to the application
$next($request) passes the request to the application
public function handle(Request $request, Closure $next) { if($request->age
< 12){ dd("你的年龄不符合要求") ; } return $next($request); }注册中间件 打开 app\Http 文件夹中的配置文件 Kernel.php $middleware 配置全局中间件,所有http请求都需要经过我们定义好的中间件。 $middlewareGroups中间件组 $routeMiddleware 定义别名 'age' =>\App\Http\Middleware\CheckAge::class Open web.php Create a route//middleware Route::get ('middleware/{age}', function () { return "Your age meets the requirements";})->middleware ('age '); The above is all the content of this article "How to customize laravel middleware", thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.