Laravel中的管道的使用实例
这篇文章主要介绍Laravel中的管道的使用实例,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

公司主营业务:网站设计、成都网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出资溪免费做网站回馈大家。
一、控制器
路由器部分
Route::get('/pipe', ['as'=>'pipe', 'uses'=>'PipeController@index']);控制代码
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下的代码如下:
三个管道的类的代码
LeftWords.php的代码
LeftWords.php的代码
BothSidesWords.php的代码这里我们使用管道默认的方法
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.jxjierui.cn/article/igcjph.html


咨询
建站咨询
