Laravel中使用管道處理名字, 實現統一處理

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

下面由laravel教程欄目給大家分享一個Laravel中的管道的使用實例,希望對需要的朋友有所幫助!

Laravel中使用管道處理名字, 實現統一處理

從代碼的角度介紹管道的實際使用方式。有關管道的說明,網上已有較多的篇幅介紹,自行查閱。
本篇博客是使用管道處理名字, 實現統一處理的目的。

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

一、控制器

路由器部分

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

二、管道部分

目錄結構如下:

├─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 = 'left-'.$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.'-right';           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 = '['.$body.']';           return $next($body);      }  }

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

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

你這樣定義后,修改你的interface,同時修改你的實現類即可。

三、結果說明

訪問http://localhost/pipe?name=lisa之后,能成功打印出獲取的結果。User表內部,有數據保存成功。

{ "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 }

? 版權聲明
THE END
喜歡就支持一下吧
點贊11 分享