Explore how Laravel's middleware is implemented
Jan 02, 2018 pm 02:05 PMHow is Laravel's middleware implemented? This article mainly introduces the implementation principle of Laravel middleware, and analyzes the concepts, principles, related methods and usage skills of Laravel middleware in more detail. Friends in need can refer to it. I hope to be helpful.
The details are as follows:
#1 What is middleware?
For a web application, before a request is actually processed, we may make various judgments on the request before it can be passed to a deeper level. And if we use if else like this, once more and more conditions need to be judged, it will make the code more difficult to maintain, and the coupling between systems will increase, and middleware can solve this problem. We can separate these judgments into middleware, which can easily filter requests.
#2 Middleware in Laravel
In Laravel, the implementation of middleware actually depends on the Illuminate\Pipeline\Pipeline class. Let’s do it first. Take a look at the code that triggers the middleware. It's very simple, just transfer the request to a closure after processing and then continue passing it on.
1 2 3 4 |
|
#3 Internal implementation of middleware
As mentioned above, middleware is implemented by Pipeline, and its call is in Illuminate\Routing\Router
1 2 3 4 5 6 7 8 9 |
|
As you can see, the middleware execution process calls three methods. Let’s take a look at the codes of these three methods:
send method
1 2 3 4 |
|
In fact, the send method does nothing, it just sets the requirements that need to be pipelined in the middleware. The object, in this case, is the HTTP request instance.
through method
1 2 3 4 |
|
The through method is also very simple, which is to set the middleware processing that needs to be processed.
then method
The really difficult thing to understand is here. The then method code is very concise, but it is not easy to understand.
1 2 3 4 5 6 7 8 9 10 11 |
|
Then there is no more, so all the middleware is passed. Isn’t it very elegant?
Since the second parameter of aray_reduce requires a function, let’s focus on the source code of the getSlice() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
You may feel dizzy when you see it. Closure returns closure. To simplify it, getSlice() returns a function A, and function A returns function B. Why return two functions? Because we use $next($request) to pass the object during the transfer process, and $next($request) means that the closure is executed. This closure is function A, and then returns function B. , which can be passed to the next middleware.
Let’s simplify the code again:
1 2 3 4 5 |
|
Look at this code again:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Look at another picture:
Each iteration passes in the previous closure and the middleware that needs to be executed. Since the array is reversed and based on the first-in-last-out feature of the stack, middleware 3 is packaged first, and middleware 1 is at the end. Outer layer. Remember, array_reduce does not execute middleware code, but wraps middleware.
You should understand after seeing this, array_reduce will eventually return func3, then call_user_func(func3,$this->passable) is actually
return call_user_func($middleware[0]-> handle, $this->passable, func2);
And the handle code in our middleware is:
1 2 3 |
|
This is equivalent to return func2($request), where $ The request is processed by the previous middleware. So the process of Zhengguo middleware is over, and it will be a bit confusing to understand. Just remember that in the end, the outermost call_user_func executes the middleware code
Related recommendations:
Laravel optimization split routing file
Laravel uses the Pagination plug-in to implement custom paging
laravel writing APP interface (API)
The above is the detailed content of Explore how Laravel's middleware is implemented. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP vs. Flutter: The best choice for mobile development

How to use object-relational mapping (ORM) in PHP to simplify database operations?

Analysis of the advantages and disadvantages of PHP unit testing tools

Comparison of the latest versions of Laravel and CodeIgniter

How do the data processing capabilities in Laravel and CodeIgniter compare?

PHP code unit testing and integration testing

Managing middleware reuse and resource sharing in the java framework
