登录  /  注册
首页 > php框架 > Laravel > 正文

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

藏色散人
发布: 2020-09-08 09:17:43
转载
2122人浏览过

下面由Laravel教程栏目给大家分享一个Laravel中的管道的使用实例,希望对需要的朋友有所帮助!

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(&#39;name&#39;);
        // $name = Str::random(10);

        return app(Pipeline::class)
            ->send($name)
            ->through($this->pipes)
            ->then(function ($content) {
                return User::create([
                    &#39;name&#39; => $content,
                    &#39;email&#39;=>Str::random(10).&#39;@gmail.com&#39;,
                    &#39;password&#39;=>Hash::make(&#39;password&#39;),
                ]);
            });
    }
}
登录后复制

二、管道部分

目录结构如下:

├─app
│  │  User.php
│  ├─Http
│  │  ...│  │
│  ├─Models
│  │  ...│  │
│  ├─Pipes
│  │  │  BothSidesWords.php
│  │  │  LeftWords.php
│  │  │  RightWords.php
│  │  │
│  │  └─Contracts
│  │          PipeContracts.php
登录后复制
  • interface的代码
    路径app/Pipes/Contracts/Pipe.php下的代码如下:

 <?php
 namespace App\Pipes\Contracts;

 use Closure;

 interface PipeContracts
 {
     public function handle($body, Closure $next);
 }
登录后复制
  • 三个管道的类的代码
    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 = &#39;left-&#39;.$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.&#39;-right&#39;;

         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 = &#39;[&#39;.$body.&#39;]&#39;;

         return $next($body);
     }
 }
登录后复制

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

return app(Pipeline::class)
        ->send($name)
        ->through($this->pipes)
        ->via(&#39;myHandleMethod&#39;)
        ->then(function ($content) {
            return User::create([
                &#39;name&#39; => $content,
                &#39;email&#39;=>Str::random(10).&#39;@gmail.com&#39;,
                &#39;password&#39;=>Hash::make(&#39;password&#39;),
            ]);
        });
登录后复制

你这样定义后,修改你的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中使用管道处理名字, 实现统一处理的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:learnku网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号