溫馨提示×

溫馨提示×

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

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

?Laravel中的管道的使用實例

發布時間:2021-01-19 10:02:30 來源:億速云 閱讀:199 作者:小新 欄目:編程語言

這篇文章主要介紹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'),
                ]);
            });
    }
}

二、管道部分

目錄結構如下:

├─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,同時修改你的實現類即可。

三、結果說明

訪問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中的管道的使用實例”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

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

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女