本篇博客是使用管道處理名字, 實(shí)現(xiàn)統(tǒng)一處理的目的。
背景:
目前能找到的使用管道的介紹也很多,大多停留在對(duì)其介紹和引導(dǎo),真正的深入到代碼的部分不多。根據(jù)介紹,使用管道也有一定的阻礙,這里分享一篇關(guān)于使用管道的詳細(xì)的代碼實(shí)例,僅供參考。
本篇介紹是自己真實(shí)使用的過(guò)程的代碼摘錄,親自測(cè)試,真實(shí)可用。只為拋磚引玉,不喜勿噴。
一、控制器
路由器部分
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,
];
// 首頁(yè)
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);
}
三個(gè)管道的類的代碼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);
}
}
這里我們使用管道默認(rèn)的方法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,同時(shí)修改你的實(shí)現(xiàn)類即可。
三、結(jié)果說(shuō)明
訪問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
}
更多關(guān)于云服務(wù)器,域名注冊(cè),虛擬主機(jī)的問題,請(qǐng)?jiān)L問西部數(shù)碼官網(wǎng):m.ps-sw.cn