首頁  >  文章  >  php框架  >  透過實例來了解Laravel中管道的使用方法

透過實例來了解Laravel中管道的使用方法

青灯夜游
青灯夜游轉載
2021-10-11 11:09:412018瀏覽

下面由Laravel教學專欄帶大家了解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中文網其他相關文章!

陳述:
本文轉載於:juejin.cn。如有侵權,請聯絡admin@php.cn刪除