欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長(zhǎng)資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          Laravel中使用管道處理名字, 實(shí)現(xiàn)統(tǒng)一處理

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

          Laravel中使用管道處理名字, 實(shí)現(xiàn)統(tǒng)一處理

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

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

          一、控制器

          路由器部分

          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,     ];     // 首頁(yè)     public function index(Request $request){         $name = $request->input('name');         // $name = Str::random(10);          return app(Pipeline::class)             ->send($name)             ->through($this->pipes)             ->then(function ($content) {                 return User::create([                     'name' => $content,                     'email'=>Str::random(10).'@gmail.com',                     'password'=>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);  }
          • 三個(gè)管道的類(lèi)的代碼
            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);      }  }

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

          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,同時(shí)修改你的實(shí)現(xiàn)類(lèi)即可。

          三、結(jié)果說(shuō)明

          訪(fǎng)問(wèn)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 }

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)