分享一个Laravel中的管道的使用实例

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

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

一、控制器

路由器部分

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

控制代码

<?php

namespace App\Http\Controllers;

use App\Pipes\LeftWords;
use App\Pipes\RightWords;
use App\Pipes\BothSidesWords;
use Illuminate\Http\Request;
use Illuminate\Pipeline\Pipeline;
use App\User;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;

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)
            ->send($name)
            ->through($this->pipes)
            ->then(function ($content) {
                return User::create([
                    'name' => $content,
                    'email'=>Str::random(10).'@gmail.com',
                    'password'=>Hash::make('password'),
                ]);
            });
    }
}

二、管道部分

目录结构如下:

├─app
│  │  User.php
│  ├─Http
│  │  ...
│  │
│  ├─Models
│  │  ...
│  │
│  ├─Pipes
│  │  │  BothSidesWords.php
│  │  │  LeftWords.php
│  │  │  RightWords.php
│  │  │
│  │  └─Contracts
│  │          PipeContracts.php
  1. interface的代码
    路径app/Pipes/Contracts/Pipe.php下的代码如下:

     <?php
     namespace App\Pipes\Contracts;
    
     use Closure;
    
     interface PipeContracts
     {
         public function handle($body, Closure $next);
     }
    
  2. 三个管道的类的代码
    LeftWords.php的代码

     <?php
     namespace App\Pipes;
    
     use App\Pipes\Contracts\PipeContracts;
     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 App\Pipes;
    
     use App\Pipes\Contracts\PipeContracts;
     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 App\Pipes;
    
     use App\Pipes\Contracts\PipeContracts;
     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
    }
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 5

其实我感觉这是装饰者模式的实现

3年前 评论

@悲剧不上演 确实有点相似,自信思考的话,还是有区别的。

装饰者:
[[[[数据]]]]
管道:
(数据1) > (数据2) > (数据3) > (数据4)

可以简单这样理解,不知道能不能理解

装饰者一般都是先把处理数据之前的函数,全部执行后。才执行数据处理之后的函数。 管道相当于每次都是把一个处理的前后函数都执行后,才流到下一个函数处理。

3年前 评论

学习了,管道部分的目录结构,是怎么生成Markdown的?

3年前 评论

@liaosp 使用三个反引号,就可以了。后面也可以加上编程语言,如:php,java,c,go,js...,大致格式如下:
```
[代码块1]
```

```php
[代码块2]
```

3年前 评论

@liaosp 那个不是 markdown 生成。 是我在cmd命令行内,使用tree /F列出全部的目录结构后,然后复制一部分放在代码格式中的。

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!