Home > PHP Framework > Laravel > body text

What is the use of laravel middleware?

WBOY
Release: 2022-04-07 16:54:00
Original
2989 people have browsed it

In laravel, the role of middleware is to filter HTTP requests and perform different logical operations according to different requests; the middleware can intercept and process the request data and inspect the data, and perform logical processing to determine whether Allows entry to the next middleware.

What is the use of laravel middleware?

#The operating environment of this article: Windows 10 system, Laravel version 6, Dell G3 computer.

What is the use of laravel's middleware

Middleware, as the name suggests, intercepts and processes request data, verifies data, and determines whether to allow entry after logical processing between the request and the response. Next middleware; middleware is divided into prefix middleware and post-middleware; it can be used for authority authentication, logging, etc.

Simply put, the role of middleware in laravel is to filter HTTP requests and perform different logical operations based on different requests.

We can achieve the following functions through middleware:

  • Specify certain routes

  • Settings HTTP response header

  • Record request

  • Filter request parameters

  • Determine whether to enable site maintenance Mode

  • Do some necessary operations before and after the response

Custom middleware

Command line You can easily create a new middleware by executing the following simple command

php artisan make:middleware <MiddlewareName>
//MiddlewareName 就是你要创建的中间件的名字
Copy after login

Execute the above command, Laravel will automatically create a middleware containing only the handle method in the app/Http/Middleware directory.

<?php
namespace App\Http\Middleware;
use Closure;
class RedirectIfSuperAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request);
    }
}
Copy after login

When the middleware is called, the handle method will be executed. What needs to be noted here is that the handle method has two parameters by default $request and $next. $request is used to accept the application's request group, and $next passes the request to the application. These two parameters are essential for handle! Middleware also includes pre-middleware and post-middleware.

"Pre-middleware" as the name suggests handles some logic before forwarding the request to the application. After middleware, on the other hand, runs after the application has processed the request and generated the response.

Pre-middleware:

<?php
namespace App\Http\Middleware;
use Closure;
class RedirectIfSuperAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //你的逻辑就在这里
        return $next($request);
    }
}
Copy after login

Post-middleware:

<?php
namespace App\Http\Middleware;
use Closure;
class RedirectIfSuperAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        //你的逻辑就在这里 例如 重定向到  `/`
        return $response;
    }
}
Copy after login

What is the use of laravel middleware?

[Related recommendations: laravel video tutorial

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template