美文网首页
分享一个Laravel中的管道的使用实例

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

作者: houxin | 来源:发表于2020-09-07 14:21 被阅读0次

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

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

一、控制器

路由器部分

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
}

相关文章

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

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

  • laravel 中pipeline管道的使用

    array_reduce的使用,利用管道可以指定的东西传递给每个任务,然后将结果返回给下一个任务,有点类似装饰器的...

  • 关于laravel make踩的坑

    laravel底层中,第一个使用make函数的地方 laravel 中make函数,就是类似于实例化,只不过lar...

  • Laravel中的管道模式

    Laravel 框架在处理Middleware中采用了一种管道模式。相信有人一定对一下的代码好奇过,$next到底...

  • Redis使用

    在使用laravel的 开源论坛中遇到这么一个问题: 问题原因在于 使用在Laravel中使用Redis时,都很喜...

  • 手写一个laravel(九) 中间件

    手写一个laravel(九) 中间件 中间件本质即为闭包 使用管道实现,核心为array_reduce函数,因为a...

  • new static 与 new self区别

    在laravel中,使用了Symfony来创建请求的,里面有这么一段 new static就是实例化自己,但是与n...

  • 知识分享之Golang——在Golang中管道(channel)

    知识分享之Golang——在Golang中管道(channel)的使用 背景 知识分享之Golang篇是我在日常使...

  • Laravel中Carbon时间格式本地化

    Laravel中Carbon时间格式本地化 Laravel使用PHP的一个扩展API–Carbon来处理时间。它提...

  • Vue中管道的使用

    在Vue2.x版本中,filter需要自己定义才能够使用过滤器可以用在两个地方: 双花括号插值,如:{{ mess...

网友评论

      本文标题:分享一个Laravel中的管道的使用实例

      本文链接:https://www.haomeiwen.com/subject/puefektx.html