本篇內容主要講解“如何使用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'),
]);
});
}
}目錄結構如下:
├─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中的管道”有了更深的了解,不妨來實際操作一番吧!這里是創新互聯網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
網站欄目:如何使用Laravel中的管道
瀏覽路徑:http://www.yijiale78.com/article36/ghdspg.html
成都網站建設公司_創新互聯,為您提供品牌網站制作、搜索引擎優化、用戶體驗、電子商務、網站導航、域名注冊
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯