This article is linked to pilishen.com—-. Welcome to learn php&Laravel with Pilishen; Study group: 109256050

This article is an extended reading of the chapter of “Laravel Underlying Core Technology Practical Revelation” and “Laravel Underlying Core Concept Analysis”. Considering the basic differences of students, in order to avoid too detailed and lengthy videos, some PHP knowledge points related to laravel’s underlying implementation are presented in the form of articles for preview and reference at any time.

> < span style = “box-sizing: border-box; color: RGB (74, 74, 74); line-height: 22px; font-size: 14px! Important; word-break: inherit! Important;”

Of course, the most direct example of a Laravel pipeline is Middleware. In this way, Middleware makes it easy to layer requests into your application.

Here’s a basic example of Middleware:

<? php namespace App\Http\Middleware; use Closure; class TestMiddleware { publicfunction handle($request, Closure $next// Add your own logic herereturn $next($request); }}Copy the code

Middleware acts as a conduit through which our requests flow, allowing us to do things like determine whether the current request is an HTTP request or a JSON request, or check the user’s permissions, This is where you might think of our laratrust (or Entrust) plugin as the classic Middleware.

In Laravel, we looked at Illuminate\Foundation\Http\Kernel class when we talked about the principle of loading and registering the service provider and interpreting the source code. SendRequestThroughRouter this method is analyzed in detail:

protected function sendRequestThroughRouter($request)
{
    $this->app->instance('request'.$request);
    Facade::clearResolvedInstance('request'); // In the video we focus on the startup process$this->bootstrap(); // The following return process is not expanded in detailreturn (new Pipeline($this->app))
                    ->send($request)
                    ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
						->then($this->dispatchToRouter());
}
Copy the code

Note the following paragraph after return:

A new Pipeline sends a request, passes through our Middleware, and then submits to the route.

Example: Implement a class logic that needs to process multiple tasks in sequence

If you are writing a forum function, users can create topics and comment on topics, but your customer requires you to implement a function that can automatically replace and delete certain content and symbols, and the data can be written to the database after processing, for example, the specific demand list is like this:

  1. Delete empty links that have no href
  2. Replace some sensitive words with asterisks*
  3. Remove potentially dangerous script tags submitted by users

Most likely, in the end, you’ll need to implement the following process:

$pipes = [
    RemoveBadWords::class
    ReplaceLinkTags::class
    RemoveScriptTags::class
];
Copy the code

What we need is to pass the content to be processed to the three classes in turn. The execution result of the last class will be passed to the next class for further processing. Then we can use pipeline to implement similar requirements:

<? php publicfunction create(Request $request)
{
    $pipes = [
        RemoveBadWords::class,
        ReplaceLinkTags::clas,
        RemoveScriptTags::class
    ];
    $post = app(Pipeline::class)
        ->send($request->content)
        ->through($pipes) - >then(function ($content) {
            return Post::create(['content'= >'content']);
        });
    // return any type of response
}
Copy the code

Each class that handles data should have a handle method that performs specific logic, so it is good to have them all extend or implement a specific interface:

<? php namespace App; use Closure; interface Pipe { publicfunction handle($content, Closure $next);
}
Copy the code

Then suppose we filter the class of sensitive words like this:

<? php namespace App; use Closure; class RemoveBadWords implements Pipe { publicfunction handle($content, Closure $next) {// The specific replacement logic is updated$content
        return  $next($content); }}Copy the code

The handle method takes two arguments, the first is the Passable object you want to pass through the pipe, and the second is a Closure, or anonymous function, that refers to the next step in your pipe. Once the logic of the current step has been executed, you can pass the result to the next step.

You can also omit the handle method name, but in this case you need to specify the custom method name via:

app(Pipeline::class)
 ->send($content)
 ->through($pipes) ->via(' customMethodName ') // <---- specify here ->then(function ($content) {
     return Post::create(['content'= >$content]);
 });
Copy the code

// Medium.com/jeffochoa/understanding-laravel-pipelines-a7191f75c351