Laravel中的Pipeline怎么使用

发布时间:2022-11-17 09:27:34 作者:iii
来源:亿速云 阅读:119

本文小编为大家详细介绍“Laravel中的Pipeline怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“Laravel中的Pipeline怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

关于管道是运行的方式,最明显的范例其实就在框架本身最常用的一个组件当中,没错,我说的就是中间件。

中间件为过滤进入应用的 HTTP 请求提供了一个便利的机制。

一个基本的中间件应该是这个样子的:

<?php
namespace App\Http\Middleware;
use Closure;
class TestMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Here you can add your code
        return $next($request);
    }
}

这些「中间件」实际上就是管道,请求经由这里发送,从而执行任何需要的任务。在这里,你可以检查请求是否是一个 HTTP 请求,是否是一个 JSON 请求,是否存在已认证的用户信息等等。

如果你想快速的查看Illuminate\Foundation\Http\Kernel 类, 你将看到如何使用 Pipeline 类的新实例来执行中间件。

/**
  * Send the given request through the middleware / router.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
protected function sendRequestThroughRouter($request)
{
    $this->app->instance('request', $request);
    Facade::clearResolvedInstance('request');
    $this->bootstrap();
    return (new Pipeline($this->app))
                    ->send($request)
                    ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
    ->then($this->dispatchToRouter());
}

你可以在代码中看到类似的内容:通过中间件列表发送请求的新管道,然后发送路由。

如果这让你看起来有点不知所措也不用担心。让我们试着用以下这个例子来阐明这个概念。

处理多任务运行类

让我们来看一种场景。 比方说,你建立了一个人们可以发帖并发表评论的论坛。但是,您的用户请求您自动删除标签或在创建时在每一个内容上编辑标签。

此时你被要求做的事情如下:

可能你最终会创建类来处理这些 “tasks”。

$pipes = [
    RemoveBadWords::class
    ReplaceLinkTags::class
    RemoveScriptTags::class
];

我们要做的是将给定的“内容”传递给每个任务,然后将结果返回给下一个任务。我们可以使用pipeline来做到这一点。

<?php

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

每个“task”类应该有一个“handle”方法来执行操作。也许每个类都有统一的约束是一个不错的选择:

<?php

namespace App;

use Closure;

interface Pipe
{
    public function handle($content, Closure $next);
}

命名是个困难的事情 ¯_(ツ)_/¯

<?php

namespace App;

use Closure;

class RemoveBadWords implements Pipe
{
    public function handle($content, Closure $next)
    {
        // Here you perform the task and return the updated $content
        // to the next pipe
        return  $next($content);
    }
}

用于执行任务的方法应该接收两个参数,第一个参数是合格的对象,第二个参数是当前操作处理完后会接管的下一个闭包(匿名函数)。

您可以使用自定义方法名称而不是“handle”。然后你需要指定pipeline要使用的方法名称,比如:

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

最后产生的效果是什么 ?

提交的内容将会被各个$pipes 所处理, 被处理的结果将会存储下来。

$post = app(Pipeline::class)
    ->send($request->all())
    ->through($pipes)
    ->then(function ($content) {
        return Post::create(['content' => $content]);
    });

读到这里,这篇“Laravel中的Pipeline怎么使用”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. redis中pipeline的介绍
  2. python中pipeline的使用方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

laravel pipeline

上一篇:vscode中如何开发一个支持vue文件跳转到定义的插件

下一篇:windows磁盘如何管理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》