cakeHP的process()函数应该运行不止一次吗



使用cakeHP中间件,我注意到函数process((可能会运行不止一次。来自日志文件的证据。我还不确定这是否会有副作用。以下示例:

class FooMiddleware implements MiddlewareInterface
{
use LogTrait;

public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface
{
$this->log('Repeat?');  
return $handler->handle($request);  
}
}

在我的日志上,我得到了相同的时间戳。

2020-04-13 23:15:21 Error: Repeat?
2020-04-13 23:15:21 Error: Repeat?

这是意料之中的事吗?

编辑

$middlewareQueue
// Catch any exceptions in the lower layers,
// and make an error page/response
->add(new ErrorHandlerMiddleware(Configure::read('Error')))
// Handle plugin/theme assets like CakePHP normally does.
->add(new AssetMiddleware([
'cacheTime' => Configure::read('Asset.cacheTime'),
]))
// Add routing middleware.
// If you have a large number of routes connected, turning on routes
// caching in production could improve performance. For that when
// creating the middleware instance specify the cache config name by
// using it's second constructor argument:
// `new RoutingMiddleware($this, '_cake_routes_')`
->add(new RoutingMiddleware($this))
->add($csrf)
->add(new AuthenticationMiddleware($this))
->add(new FooMiddleware())
->add(new AuthorizationMiddleware($this));
if (Configure::read('debug')) {
// Disable authz for debugkit
$middlewareQueue->add(function ($req, $res, $next) {
if ($req->getParam('plugin') === 'DebugKit') {
$req->getAttribute('authorization')->skipAuthorization();
}
return $next($req, $res);
});
}
return $middlewareQueue;

不,它不应该运行多次,除非它多次包含在中间件队列中。

做一些额外的调试来了解发生了什么,例如记录stacktrace:

$this->log(CakeErrorDebugger::trace());

并登录webroot/index.php

CakeLogLog::write(PsrLogLogLevel::ERROR, 'index.php hit');

这应该揭示中间件是被多次调用(以及从哪里调用(,还是实际上是多个请求。

最新更新