最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Laravel中使用管道处理名字, 实现统一处理

来源:懂视网 责编:小采 时间:2020-11-02 18:24:37
文档

Laravel中使用管道处理名字, 实现统一处理

Laravel中使用管道处理名字, 实现统一处理: 下面由Laravel教程栏目给大家分享一个Laravel中的管道的使用实例,希望对需要的朋友有所帮助!从代码的角度介绍管道的实际使用方式。有关管道的说明,网上已有较多的篇幅介绍,自行查阅。本篇博客是使用管道处理名字, 实现统一处理的目的。背景:目前能找到
推荐度:
导读Laravel中使用管道处理名字, 实现统一处理: 下面由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)
 ->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
  • 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
    }

    文档

    Laravel中使用管道处理名字, 实现统一处理

    Laravel中使用管道处理名字, 实现统一处理: 下面由Laravel教程栏目给大家分享一个Laravel中的管道的使用实例,希望对需要的朋友有所帮助!从代码的角度介绍管道的实际使用方式。有关管道的说明,网上已有较多的篇幅介绍,自行查阅。本篇博客是使用管道处理名字, 实现统一处理的目的。背景:目前能找到
    推荐度:
    标签: 名字 使用 处理
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top