The use of Pipeline design patterns in Laravel-exploring the principle of Middleware implementation the so-called Pipeline design pattern is to transfer data to a task sequence, and the Pipeline plays the role of the Pipeline, the data is processed here and then transmitted to the next step.
Pipeline has many advantages, especially when writing complex processing code in a single task, in addition, you can easily add, remove, or replace tasks in the MPs queue.
Laravel uses the Pipeline design pattern in many aspects of the framework, which means that all the places where we need to implement the Pipeline design pattern are already part of the underlying application.
We can use the internal components of Laravel to build our own functions at the framework layer. In today's tutorial, we will discuss the pipeline design patterns and how to use the Laravel internal pipeline.
What is the pipeline design model?
The pipeline mode is used to break complex processes into multiple independent subtasks. Each independent task is reusable, so these tasks can be combined into complex processes.
This mode allows you to break down large processes into smaller subtasks, which process data and pass the processed results to the next subtask. Like an assembly line, it is well organized to implement a complete process from raw material processing to finished products.
Each task in the MPs queue accepts and returns data of the same type, so that the task can be added, removed, or replaced in the MPs queue without affecting other subtasks.
If you are familiar with Unix systems, you may be familiar with pipelines, because we often use pipeline commands in shell commands, such:
cat helloworld.txt | grep "hello world" | rev | > output.txt
In this example, we read the file content, query the string "hello world", reverse the string, and add it to the output.txt file.
In Laravel, how do I use the pipe mode?
Laravel uses the pipeline design mode in many aspects of the framework. The most common is the implementation of middleware.
Before the request reaches the controller action to be processed, it goes through a series of middleware. Each medium price has an independent responsibility, such as setting cookies, determining whether to log on, and blocking CSRF attacks.
Each stage processes the request. if the request passes, it is passed to the next Processing. if the request fails, the corresponding HTTP response is returned.
This mechanism makes it easy for us to add processing operations before the request finally reaches the application code. of course, if you do not need this processing operation, you can also remove it at any time without affecting the request lifecycle.
Advantages of MPs queue mode
Pipeline mode has many advantages:
First, the complex processing process is divided into independent subtasks to facilitate testing each subtask;
Second, the decomposed sub-tasks can be reused by different processing processes to avoid code redundancy.
Finally, it is easy to add, remove, and replace subtasks in a complex process without affecting existing processes.
Disadvantages of MPs queue mode
Of course, the pipeline model also has the following disadvantages:
Although each sub-task becomes simple, it is complicated when you try to combine these sub-tasks into a complete process;
In addition, you also need to ensure that the entire process can work normally after the test of the independent subtask is passed, which has certain uncertainty.
Finally, when you see all the sub-tasks, it is difficult to understand the overall process (the story of a blind person may be very familiar to everyone, and this is exactly the case ).
How to use the Laravel pipeline
The Pipeline provided by Laravel is simple. First, you need to create a new Illuminate \ Pipeline object and inject it into an instance of Illuminate \ Contracts \ Container:
$pipeline = app('Illuminate\Pipeline\Pipeline');
Next, send the object you want to pass to this pipeline:
$pipeline->send($request);
Then pass it to the task array that accepts and processes the request:
$pipeline->through($middleware);
Finally, run the MPs queue task and write the callback:
$pipeline->then(function ($request) { // Do something});
This is the basic working principle of middleware: to receive HTTP requests, let the requests go through the defined routing middleware, and finally reach the destination for processing.
Conclusion
The pipeline design pattern is very useful. middleware is just a special example. Laravel makes full use of this design pattern in the underlying framework. of course, you can also use the pipeline provided by Laravel in your own project.
If you want to use pipelines that do not depend on any framework, refer to this Github project: League \ Pipeline
Declaration: This article is an English translation. Original article: How to use the Pipeline Design Pattern in Laravel