Home PHP Framework Laravel Detailed explanation of middleware in laravel framework

Detailed explanation of middleware in laravel framework

Oct 15, 2018 pm 02:43 PM
laravel php

This article brings you a detailed explanation of the middleware of the laravel framework. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Laravel middleware is a very convenient thing, which can decouple some logic implementations, and in laravel,
The writing of middleware is also very convenient. Who is who knows.

1. Decorator mode

The middleware in laravel uses the decorator mode. What is [decorator mode][1], let’s learn about it first Well, let’s briefly talk about it here. This model is mainly used to solve the problem. When a class needs to dynamically extend functions, using inheritance will expand the subclass, and the extended function is a public function, which is not conducive to the problem. Function reuse and code decoupling.

In laravel, the function that uses this mode is called the request processing pipeline, which is pipeline

//公共接口
interface middleware {
        public static function handle(Closure $next);
    }
//装饰器1
class MiddleStepOne implements middleware{
        public static function handle(Closure $next) {
            echo "前期处理的第一步"."<br>";
            $next();
            echo "后期处理的第一步"."<br>";
        }
    }
//装饰器2
class MiddleStepTwo implements middleware{
    public static function handle(Closure $next) {
        echo "前期处理的第二步"."<br>";
        $next();
        echo "后期处理的第二步"."<br>";
    }
}

function goFunc() {
    return function ($step,$className) {
      return function () use ($step,$className) {
          return $className::handle($step);
      };
    };
}

$pip = array(
    MiddleStepOne::class,
    MiddleStepTwo::class,
);
$pip = array_reverse($pip);  //反转数组,以求达到要求的顺序运行
$first = function (){
    echo "前期处理完毕"."<br>";
};  //实际要处理的函数
$a = array_reduce($pip,goFunc(),$first); //遍历pip数组,并将first作为第一个参数传递进去
$a(); //执行
Copy after login

Output:

Detailed explanation of middleware in laravel framework

This is a simple pipeline based on the decorator pattern. Its essence is actually based on closure and recursion.

By analyzing this program, for the finally generated $a variable, its value is probably like MiddleStepOne.handle(MiddleStepTwo.handle(first)). When executed, because there is next( in the handle ) function exists, so this is a recursive call. For laravel's middleware, its implementation principle is the same as this.

2. Middleware and request processing pipeline in laravel

In laravel, we can do some pre-processing before request execution by setting middleware .

Start from the request entrance public/index.php

Detailed explanation of middleware in laravel framework

##The important thing is this code: that is, processing the request, Return the response to the request


$response = $kernel->handle(
$request = Illuminate\Http\Request::capture() //创建一个请求实例
);
Copy after login
Then we enter the kernel to see its specific implementation in IlluminateFoundationHttpKernel.php

Detailed explanation of middleware in laravel framework

Detailed explanation of middleware in laravel frameworkPlease read it yourself about the dispatchToRouter() function, so I won’t go into details here.

The next step is the exciting PipeLine class.

<?php namespace Illuminate\Pipeline;

use Closure;
use RuntimeException;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Pipeline\Pipeline as PipelineContract;

class Pipeline implements PipelineContract
{
    /**
     * The container implementation.
     *
     * @var \Illuminate\Contracts\Container\Container
     */
    protected $container;

    /**
     * The object being passed through the pipeline.
     *
     * @var mixed
     */
    protected $passable;

    /**
     * The array of class pipes.
     *
     * @var array
     */
    protected $pipes = [];

    /**
     * The method to call on each pipe.
     *
     * @var string
     */
    protected $method = &#39;handle&#39;;

    /**
     * Create a new class instance.
     *
     * @param  \Illuminate\Contracts\Container\Container|null  $container
     * @return void
     */
    public function __construct(Container $container = null)
    {
        $this->container = $container;
    }

    /**
     * Set the object being sent through the pipeline.
     *
     * @param  mixed  $passable
     * @return $this
     */
    public function send($passable)
    {
        $this->passable = $passable;

        return $this;
    }

    /**
     * Set the array of pipes.
     *
     * @param  array|mixed  $pipes
     * @return $this
     */
    public function through($pipes)
    {
        $this->pipes = is_array($pipes) ? $pipes : func_get_args();

        return $this;
    }

    /**
     * Set the method to call on the pipes.
     *
     * @param  string  $method
     * @return $this
     */
    public function via($method)
    {
        $this->method = $method;

        return $this;
    }

    /**
     * Run the pipeline with a final destination callback.
     *
     * @param  \Closure  $destination
     * @return mixed
     */
    public function then(Closure $destination)
    {
        $pipeline = array_reduce(
            array_reverse($this->pipes), $this->carry(), $this->prepareDestination($destination)
        );

        return $pipeline($this->passable);
    }

    /**
     * Get the final piece of the Closure onion.
     *
     * @param  \Closure  $destination
     * @return \Closure
     */
    protected function prepareDestination(Closure $destination)
    {
        return function ($passable) use ($destination) {
            return $destination($passable);
        };
    }

    /**
     * Get a Closure that represents a slice of the application onion.
     *
     * @return \Closure
     */
    protected function carry()
    {
        return function ($stack, $pipe) {
            return function ($passable) use ($stack, $pipe) {
                if (is_callable($pipe)) {
                    // If the pipe is an instance of a Closure, we will just call it directly but
                    // otherwise we'll resolve the pipes out of the container and call it with
                    // the appropriate method and arguments, returning the results back out.
                    //如果pip也就中间件函数是一个闭包可调用函数,就直接返回这个闭包函数就行了
                    //这里我还没有找到对应的使用场景,后续补充
                    return $pipe($passable, $stack);
                } elseif (! is_object($pipe)) {
                    list($name, $parameters) = $this->parsePipeString($pipe);

                    // If the pipe is a string we will parse the string and resolve the class out
                    // of the dependency injection container. We can then build a callable and
                    // execute the pipe function giving in the parameters that are required.
                    $pipe = $this->getContainer()->make($name);

                    $parameters = array_merge([$passable, $stack], $parameters);
                } else {
                    // If the pipe is already an object we'll just make a callable and pass it to
                    // the pipe as-is. There is no need to do any extra parsing and formatting
                    // since the object we're given was already a fully instantiated object.
                    $parameters = [$passable, $stack];
                }

                return method_exists($pipe, $this->method)
                                ? $pipe->{$this->method}(...$parameters)
                                : $pipe(...$parameters);
            };
        };
    }

    /**
     * Parse full pipe string to get name and parameters.
     *
     * @param  string $pipe
     * @return array
     */
    protected function parsePipeString($pipe)
    {
        list($name, $parameters) = array_pad(explode(':', $pipe, 2), 2, []);

        if (is_string($parameters)) {
            $parameters = explode(',', $parameters);
        }

        return [$name, $parameters];
    }

    /**
     * Get the container instance.
     *
     * @return \Illuminate\Contracts\Container\Container
     * @throws \RuntimeException
     */
    protected function getContainer()
    {
        if (! $this->container) {
            throw new RuntimeException('A container instance has not been passed to the Pipeline.');
        }

        return $this->container;
    }
}
Copy after login
In general, the implementation of the pipeLine class is similar to the decorator I wrote before. The main trouble here is that

protected function Inside the carry() function, the processing of pip is a closure, string, and object.


I used to think that laravel's middleware is a very mysterious thing, but after reading it, I realized that it is just like that, very sophisticated. This model is also very helpful in actual development. For example, we are currently A gateway project was used. Since no framework was used, the judgment conditions were stripped off and written into middleware, thus achieving a certain degree of modular programming.

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

Top 10 PHP CMS Platforms For Developers in 2024 Top 10 PHP CMS Platforms For Developers in 2024 Dec 05, 2024 am 10:29 AM

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

In Laravel, how to deal with the situation where verification codes are failed to be sent by email? In Laravel, how to deal with the situation where verification codes are failed to be sent by email? Mar 31, 2025 pm 11:48 PM

The method of handling Laravel's email failure to send verification code is to use Laravel...

See all articles