溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Laravel中的Pipeline怎么使用

發(fā)布時(shí)間:2022-11-17 09:27:34 來源:億速云 閱讀:138 作者:iii 欄目:編程語言

本文小編為大家詳細(xì)介紹“Laravel中的Pipeline怎么使用”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Laravel中的Pipeline怎么使用”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。

關(guān)于管道是運(yùn)行的方式,最明顯的范例其實(shí)就在框架本身最常用的一個(gè)組件當(dāng)中,沒錯(cuò),我說的就是中間件。

中間件為過濾進(jìn)入應(yīng)用的 HTTP 請(qǐng)求提供了一個(gè)便利的機(jī)制。

一個(gè)基本的中間件應(yīng)該是這個(gè)樣子的:

<?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);
    }
}

這些「中間件」實(shí)際上就是管道,請(qǐng)求經(jīng)由這里發(fā)送,從而執(zhí)行任何需要的任務(wù)。在這里,你可以檢查請(qǐng)求是否是一個(gè) HTTP 請(qǐng)求,是否是一個(gè) JSON 請(qǐng)求,是否存在已認(rèn)證的用戶信息等等。

如果你想快速的查看Illuminate\Foundation\Http\Kernel 類, 你將看到如何使用 Pipeline 類的新實(shí)例來執(zhí)行中間件。

/**
  * 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());
}

你可以在代碼中看到類似的內(nèi)容:通過中間件列表發(fā)送請(qǐng)求的新管道,然后發(fā)送路由。

如果這讓你看起來有點(diǎn)不知所措也不用擔(dān)心。讓我們?cè)囍靡韵逻@個(gè)例子來闡明這個(gè)概念。

處理多任務(wù)運(yùn)行類

讓我們來看一種場(chǎng)景。 比方說,你建立了一個(gè)人們可以發(fā)帖并發(fā)表評(píng)論的論壇。但是,您的用戶請(qǐng)求您自動(dòng)刪除標(biāo)簽或在創(chuàng)建時(shí)在每一個(gè)內(nèi)容上編輯標(biāo)簽。

此時(shí)你被要求做的事情如下:

  • 用純文本替換鏈接標(biāo)記;

  • 用“*”替換敏感詞;

  • 從內(nèi)容中完全刪除腳本標(biāo)記。

可能你最終會(huì)創(chuàng)建類來處理這些 “tasks”。

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

我們要做的是將給定的“內(nèi)容”傳遞給每個(gè)任務(wù),然后將結(jié)果返回給下一個(gè)任務(wù)。我們可以使用pipeline來做到這一點(diǎn)。

<?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
}

每個(gè)“task”類應(yīng)該有一個(gè)“handle”方法來執(zhí)行操作。也許每個(gè)類都有統(tǒng)一的約束是一個(gè)不錯(cuò)的選擇:

<?php

namespace App;

use Closure;

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

命名是個(gè)困難的事情 ˉ_(ツ)_/ˉ

<?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);
    }
}

用于執(zhí)行任務(wù)的方法應(yīng)該接收兩個(gè)參數(shù),第一個(gè)參數(shù)是合格的對(duì)象,第二個(gè)參數(shù)是當(dāng)前操作處理完后會(huì)接管的下一個(gè)閉包(匿名函數(shù))。

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

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

最后產(chǎn)生的效果是什么 ?

提交的內(nèi)容將會(huì)被各個(gè)$pipes 所處理, 被處理的結(jié)果將會(huì)存儲(chǔ)下來。

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

讀到這里,這篇“Laravel中的Pipeline怎么使用”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI