当前位置:网站首页>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) {
});
案例
边栏推荐
- 365 day challenge leetcode 1000 questions - day 035 one question per day + two point search 13
- 记xx公司SQL注入漏洞
- DAY15:文件包含漏洞靶场手册(自用 file-include 靶场)
- Flask 报错 RuntimeError: The session is unavailable because no secret key was set.
- ClickHouse学习(八)物化视图
- [electronic circuit] how to select ADC chip
- Clickhouse learning (x) monitoring operation indicators
- Topological ordering of a graph of water
- Clickhouse learning (VI) grammar optimization
- 组件传参与生命周期
猜你喜欢

uniapp组件之tab选项卡滑动切换

WIN10 编译ffmpeg(包含ffplay)

About local variables

Flask 报错 RuntimeError: The session is unavailable because no secret key was set.

DAY6:利用 PHP 编写文件上传页面

解决表单校验提示信息不消失问题以及赋值不生效问题

Day14: upload labs customs clearance tutorial

Summary of knowledge points related to forms and forms

【电子电路】ADC芯片如何选型

eggjs 创建应用知识点
随机推荐
HCIA-R&S自用笔记(24)ACL
QT layout management -- Part stretch principle and sizepolicy
link与@import的关系
[typescript] learn typescript object types in depth
【JS题解】牛客网JS篇1-10题
Fvuln-自动化web漏洞检测工具
[typescript] type reduction (including type protection) and type predicate in typescript
Basic use of redis
用threejs 技术做游戏跑酷
实现table某个单元格背景色设置
2022 mathematical modeling competition summer training lecture - optimization method: goal planning
What is wapiti and how to use it
ClickHouse学习(十一)clickhouseAPI操作
Wapiti是什么以及使用教程
Qtcreator+cmake compiler settings
Clickhouse learning (XI) clickhouseapi operation
What is nmap and how to use it
相对定位和绝对定位
QPalette学习笔记
[sword finger offer] - explain the library function ATOI and simulate the realization of ATOI function