通過實例來了解Laravel中管道的使用方法

下面由laravel教程欄目帶大家了解一下laravel中的管道,分享一個laravel中的管道的使用實例,希望對大家有所幫助!

通過實例來了解Laravel中管道的使用方法

從代碼的角度介紹管道的實際使用方式。有關(guān)管道的說明,網(wǎng)上已有較多的篇幅介紹,自行查閱。 本篇博客是使用管道處理名字, 實現(xiàn)統(tǒng)一處理的目的。

背景: 目前能找到的使用管道的介紹也很多,大多停留在對其介紹和引導,真正的深入到代碼的部分不多。根據(jù)介紹,使用管道也有一定的阻礙,這里分享一篇關(guān)于使用管道的詳細的代碼實例,僅供參考。 本篇介紹是自己真實使用的過程的代碼摘錄,親自測試,真實可用。只為拋磚引玉,不喜勿噴。

一、控制器

路由器部分

Route::get('/pipe',?['as'=>'pipe',?'uses'=>'PipeController@index']);

控制代碼

<?php namespace ApphttpControllers;  use AppPipesLeftWords; use AppPipesRightWords; use AppPipesBothSidesWords; use IlluminateHttpRequest; use IlluminatePipelinePipeline; use AppUser; use IlluminateSupportStr; use IlluminateSupportFacadesHash;  class PipeController extends Controller {     /* 定義管道      *      * 第一步處理      * 第二部處理      * 第三部處理      * */     protected $pipes = [         LeftWords::class,         RightWords::class,         BothSidesWords::class,     ];     // 首頁     public function index(Request $request){         $name = $request->input('name'); ????????//?$name?=?Str::random(10);  ????????return?app(Pipeline::class) ????????????-&gt;send($name) ????????????-&gt;through($this-&gt;pipes) ????????????-&gt;then(function?($content)?{ ????????????????return?User::create([ ????????????????????'name'?=&gt;?$content, ????????????????????'email'=&gt;Str::random(10).'@gmail.com', ????????????????????'password'=&gt;Hash::make('password'), ????????????????]); ????????????}); ????} }

二、管道部分

目錄結(jié)構(gòu)如下:

├─app │??│??User.php │??├─Http │??│??... │??│ │??├─Models │??│??... │??│ │??├─Pipes │??│??│??BothSidesWords.php │??│??│??LeftWords.php │??│??│??RightWords.php │??│??│ │??│??└─Contracts │??│??????????PipeContracts.php
  • Interface的代碼 路徑app/Pipes/Contracts/Pipe.php下的代碼如下:

    <?php namespace AppPipesContracts;  use Closure;  interface PipeContracts {     public function handle($body, Closure $next); }
  • 三個管道的類的代碼LeftWords.php的代碼

    <?php namespace AppPipes;  use AppPipesContractsPipeContracts; use Closure;  class LeftWords implements PipeContracts{     public function handle($body, Closure $next)     {         // TODO: Implement handle() method.          $body = &#39;left-&#39;.$body;          return $next($body);     } }

    LeftWords.php的代碼

    <?php namespace AppPipes;  use AppPipesContractsPipeContracts; use Closure;  class RightWords implements PipeContracts{     public function handle($body, Closure $next)     {         // TODO: Implement handle() method.          $body = $body.&#39;-right&#39;;          return $next($body);     } }

    BothSidesWords.php的代碼

    <?php namespace AppPipes;  use AppPipesContractsPipeContracts; use Closure;  class BothSidesWords implements PipeContracts{     public function handle($body, Closure $next)     {         // TODO: Implement handle() method.          $body = &#39;[&#39;.$body.&#39;]&#39;;          return $next($body);     } }

這里我們使用管道默認的方法handle,你可以自定義方法名。像下面這樣定義myHandleMethod為處理方法名稱。

return?app(Pipeline::class) 	???????-&gt;send($name) 	???????-&gt;through($this-&gt;pipes) 	???????-&gt;via('myHandleMethod') 	???????-&gt;then(function?($content)?{ 	???????????return?User::create([ 	???????????????'name'?=&gt;?$content, 	???????????????'email'=&gt;Str::random(10).'@gmail.com', 	???????????????'password'=&gt;Hash::make('password'), 	???????????]); 	???????});

你這樣定義后,修改你的interface,同時修改你的實現(xiàn)類即可。

三、結(jié)果說明

訪問http://localhost/pipe?name=lisa之后,能成功打印出獲取的結(jié)果。User表內(nèi)部,有數(shù)據(jù)保存成功。

{ "name":?"[left-lisa-right]", "email":?"3riSrDuBFv@gmail.com", "updated_at":?"2020-09-05T05:57:14.000000Z", "created_at":?"2020-09-05T05:57:14.000000Z", "id":?15 }

更多編程相關(guān)知識,請訪問:laravel!!

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊13 分享