您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

一文了解Laravel中的Pipeline(管道)

2025/6/8 17:19:05发布9次查看
本篇文章带大家了解一下laravel中的pipeline(管道),聊聊管道设计范式,希望对大家有所帮助!
总的来说,通过使用 laravel 中的管道,你能够流畅地在若干个类之间传递一个对象,从而执行一个任意类型的任务,一旦所有的任务都被执行完,就会将结果值返回。
接下来,你能了解到更多关于 laravel pipelines 的知识。
关于管道是运行的方式,最明显的范例其实就在框架本身最常用的一个组件当中,没错,我说的就是中间件。
中间件为过滤进入应用的 http 请求提供了一个便利的机制。
一个基本的中间件应该是这个样子的:
<?phpnamespace 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来做到这一点。
<?phppublic 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”方法来执行操作。也许每个类都有统一的约束是一个不错的选择:
<?phpnamespace app;use closure;interface pipe{ public function handle($content, closure $next);}
命名是个困难的事情 ¯_(ツ)_/¯
<?phpnamespace 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 pipelines”,以及如何使用它们。如果你想了解或者学习更多,你可以查看laravel的api文档laravel.com/api/5.4/illuminate/pip...
原文地址:https://medium.com/@jeffochoa/understanding-laravel-pipelines-a7191f75c351
译文地址:https://learnku.com/laravel/t/7543/pipeline-pipeline-design-paradigm-in-laravel
【相关推荐:laravel视频教程】
以上就是一文了解laravel中的pipeline(管道)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product