当前位置:网站首页>Thinkphp6管道模式Pipeline使用
Thinkphp6管道模式Pipeline使用
2022-07-29 05:19:00 【廖圣平】
导读
纵观网络上使用Tp的管道模式的例子非常少,都是解析在框架中中间件使用应用场景,其实在业务开发中管道模式也非常好用。
网络上大部分搜到的Laravel 的例子,但是Laravel 的例子中在Tp是无法实现的,经过摸索,终于搞定了Tp的使用

查看官网更是没有人讨论管道模式,而在Learnku 一大堆人讨论,由此说明了 框架是有可比性的。
于是我翻遍了源码,从Think 中间件的单元测试,到中间件的加载过程,终于发现了这边的格式,是长这样子的:
/** * 解析中间件 * @access protected * @param mixed $middleware * @param string $type 中间件类型 * @return array */
protected function buildMiddleware($middleware, string $type): array
{
if (is_array($middleware)) {
[$middleware, $params] = $middleware;
}
if ($middleware instanceof Closure) {
return [$middleware, $params ?? []];
}
if (!is_string($middleware)) {
throw new InvalidArgumentException('The middleware is invalid');
}
//中间件别名检查
$alias = $this->app->config->get('middleware.alias', []);
if (isset($alias[$middleware])) {
$middleware = $alias[$middleware];
}
if (is_array($middleware)) {
$this->import($middleware, $type);
return [];
}
return [[$middleware, 'handle'], $params ?? []];
}
它这边需要传 handle 方法,一般Laravel 和其他主流的框架中的管道都是默认 handle 方法,不能说这样的设计不好,但是起码也得有个默认方法,鬼知道没有文档的情况下这样写。
准备
class Test
{
public static function handle($request,$next){
echo '我是中间件1';
$next($request);
}
}
class Test2
{
public static function handle($request,$next){
$next($request);
echo '我是中间件222';
}
}
class Request
{
}
使用
$request = new Request();
$pips[] = [\app\common\service\order\pipeline\Test::class,'handle'];
$pips[] = [\app\common\service\order\pipeline\Test2::class,'handle'];
(new Pipeline())
->send($request)
->through($pips)
->then(function ($e) {
});
案例
边栏推荐
- Build msys2 environment with win10
- [C language series] - three methods to simulate the implementation of strlen library functions
- 微信小程序-屏幕高度
- Win10 搭建MSYS2环境
- JS simple code determines whether the device that opens the page is the PC end of the computer, the H5 end of the mobile phone, or the wechat end
- WIN10 编译ffmpeg(包含ffplay)
- 用sql-client.sh生成的job在cancle过后 如何实现断点续传?
- Record the SQL injection vulnerability of XX company
- 【JS题解】牛客网JS篇1-10题
- What is nmap and how to use it
猜你喜欢
随机推荐
[JS question solution] questions 1-10 in JS of niuke.com
href与src的区别
Hcia-r & s self use notes (24) ACL
Detailed explanation of serial port communication
Related knowledge of elastic box
实现table某个单元格背景色设置
Qt设置背景图片方法
Wechat applet change attribute value -setdata- bidirectional binding -model
Merge the same items in the same column in table
[C language series] - three methods to simulate the implementation of strlen library functions
【TypeScript】TypeScript中类型缩小(含类型保护)与类型谓词
Three handshakes and four waves for the interview summary
用threejs 技术做游戏跑酷
[untitled]
表格与表单相关知识点总结
[C language series] - a recursive topic
JS deep copy - Notes
【JS题解】牛客网JS篇1-10题
DAY14:Upload-labs 通关教程
Database operation day 6






![[typescript] type reduction (including type protection) and type predicate in typescript](/img/74/52fe769ed3850e01d97cb9fefb7373.png)


