溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用Laravel中的管道

發(fā)布時間:2021-10-11 11:35:33 來源:億速云 閱讀:143 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“如何使用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('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'),
                ]);
            });
    }
}

二、管道部分

目錄結(jié)構(gòu)如下:

├─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 = '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,同時修改你的實現(xiàn)類即可。

三、結(jié)果說明

訪問http://localhost/pipe?name=lisa之后,能成功打印出獲取的結(jié)果。User表內(nèi)部,有數(shù)據(jù)保存成功。

{
"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中的管道”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI