通过实例来了解Laravel中管道的使用方法

 5027

下面带大家了解一下Laravel中的管道,分享一个Laravel中的管道的使用实例,希望对大家有所帮助!


通过实例来了解Laravel中管道的使用方法


从代码的角度介绍管道的实际使用方式。有关管道的说明,网上已有较多的篇幅介绍,自行查阅。 本篇博客是使用管道处理名字, 实现统一处理的目的。

背景: 目前能找到的使用管道的介绍也很多,大多停留在对其介绍和引导,真正的深入到代码的部分不多。根据介绍,使用管道也有一定的阻碍,这里分享一篇关于使用管道的详细的代码实例,仅供参考。 本篇介绍是自己真实使用的过程的代码摘录,亲自测试,真实可用。只为抛砖引玉,不喜勿喷。

一、控制器

路由器部分

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

控制代码

  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Pipes\LeftWords;
  6. use App\Pipes\RightWords;
  7. use App\Pipes\BothSidesWords;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Pipeline\Pipeline;
  10. use App\User;
  11. use Illuminate\Support\Str;
  12. use Illuminate\Support\Facades\Hash;
  13.  
  14. class PipeController extends Controller
  15. {
  16.     /* 定义管道
  17.      *
  18.      * 第一步处理
  19.      * 第二部处理
  20.      * 第三部处理
  21.      * */
  22.     protected $pipes = [
  23.         LeftWords::class,
  24.         RightWords::class,
  25.         BothSidesWords::class,
  26.     ];
  27.     // 首页
  28.     public function index(Request $request){
  29.         $name = $request->input('name');
  30.         // $name = Str::random(10);
  31.  
  32.         return app(Pipeline::class)
  33.             ->send($name)
  34.             ->through($this->pipes)
  35.             ->then(function ($content) {
  36.                 return User::create([
  37.                     'name' => $content,
  38.                     'email'=>Str::random(10).'@gmail.com',
  39.                     'password'=>Hash::make('password'),
  40.                 ]);
  41.             });
  42.     }
  43. }


二、管道部分

目录结构如下:

  1. ├─app
  2. │  │  User.php
  3. │  ├─Http
  4. │  │  ...
  5. │  │
  6. │  ├─Models
  7. │  │  ...
  8. │  │
  9. │  ├─Pipes
  10. │  │  │  BothSidesWords.php
  11. │  │  │  LeftWords.php
  12. │  │  │  RightWords.php
  13. │  │  │
  14. │  │  └─Contracts
  15. │  │          PipeContracts.php

interface的代码 路径app/Pipes/Contracts/Pipe.php下的代码如下:

  1. <?php
  2. namespace App\Pipes\Contracts;
  3.  
  4. use Closure;
  5.  
  6. interface PipeContracts
  7. {
  8.     public function handle($body, Closure $next);
  9. }

三个管道的类的代码LeftWords.php的代码

  1. <?php
  2. namespace App\Pipes;
  3.  
  4. use App\Pipes\Contracts\PipeContracts;
  5. use Closure;
  6.  
  7. class LeftWords implements PipeContracts{
  8.     public function handle($body, Closure $next)
  9.     {
  10.         // TODO: Implement handle() method.
  11.  
  12.         $body = 'left-'.$body;
  13.  
  14.         return $next($body);
  15.     }
  16. }

LeftWords.php的代码

  1. <?php
  2. namespace App\Pipes;
  3.  
  4. use App\Pipes\Contracts\PipeContracts;
  5. use Closure;
  6.  
  7. class RightWords implements PipeContracts{
  8.     public function handle($body, Closure $next)
  9.     {
  10.         // TODO: Implement handle() method.
  11.  
  12.         $body = $body.'-right';
  13.  
  14.         return $next($body);
  15.     }
  16. }

BothSidesWords.php的代码

  1. <?php
  2. namespace App\Pipes;
  3.  
  4. use App\Pipes\Contracts\PipeContracts;
  5. use Closure;
  6.  
  7. class BothSidesWords implements PipeContracts{
  8.     public function handle($body, Closure $next)
  9.     {
  10.         // TODO: Implement handle() method.
  11.  
  12.         $body = '['.$body.']';
  13.  
  14.         return $next($body);
  15.     }
  16. }

这里我们使用管道默认的方法handle,你可以自定义方法名。像下面这样定义myHandleMethod为处理方法名称。

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

你这样定义后,修改你的interface,同时修改你的实现类即可。


三、结果说明

访问http://localhost/pipe?name=lisa之后,能成功打印出获取的结果。User表内部,有数据保存成功。

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


TAG标签:
本文网址:https://www.zztuku.com/index.php/detail-9542.html
站长图库 - 通过实例来了解Laravel中管道的使用方法
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐