Home  >  Article  >  PHP Framework  >  Detailed explanation of laravel middleware basics

Detailed explanation of laravel middleware basics

WBOY
WBOYforward
2022-05-18 11:46:242968browse

This article brings you relevant knowledge about laravel, which mainly introduces related issues about middleware, including what is middleware, custom middleware, etc., middleware It provides a convenient mechanism for filtering HTTP requests entering the application. Let's take a look at it. I hope it will be helpful to everyone.

Detailed explanation of laravel middleware basics

【Related recommendations: laravel video tutorial

What is middleware

Middleware Provides a convenient mechanism for filtering HTTP requests entering an application. For example, Laravel has a built-in middleware to verify whether the user is authenticated (such as logged in). If the user is not authenticated, the middleware will redirect the user to the login page; if the user has been authenticated, the middleware will allow the request. Continue forward 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) to responses leaving the site; log middleware can record all requests entering the site, making it easier for us to build a system log system.

Lavarel comes with some middleware, including authentication, CSRF protection middleware, etc. All middleware is located in the app\Http\Middleware directory.

1. Customize middleware

Steps:

  1. Use artisan command to create middlewarephp artisan make:middleware middleware name
  2. Write logic in the created middleware
  3. Register the middleware in the configuration file Kernel.php in the app\Http folder
  4. Assign the middleware to Router or controller

2. For example

For example, some items in the amusement park require 12 years old or a height of more than 1.4 meters to play.

We create a middleware for age verification

php artisan make:middleware CheckAge

Detailed explanation of laravel middleware basics

Closure is a closure function
$request is used to receive application requests Array
$next Pass the request to the application
$next($request) Pass the request to the application

 public function handle(Request $request, Closure $next)
    {
        if($request->age <h3>Register middleware</h3><p>Open app\Http The configuration file Kernel.php</p><blockquote><p>$middleware in the folder configures global middleware. All http requests need to go through the middleware we defined. <br> $middlewareGroupsMiddleware Group<br> $routeMiddleware Define alias</p></blockquote><pre class="brush:php;toolbar:false">'age' => \App\Http\Middleware\CheckAge::class

Detailed explanation of laravel middleware basics

Open web.php to create a route

//中间件Route::get('middleware/{age}',function () {
    return "你的年龄符合要求";})->middleware('age');

Test

http://www.la.com/middleware/1
Detailed explanation of laravel middleware basics

http://www.la.com/middleware/12
Detailed explanation of laravel middleware basics

【Related recommendations: laravel video tutorial

The above is the detailed content of Detailed explanation of laravel middleware basics. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete