【发布时间】:2020-04-14 06:53:40
【问题描述】:
使用 cakePHP 中间件,我注意到函数 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;
【问题讨论】:
标签: cakephp middleware