【发布时间】:2017-08-12 13:26:43
【问题描述】:
在我的 Slim 3 应用程序中,我定义了一个中间件,它为我的响应添加了一个自定义标头。 在索引路由函数被调用之前调用中间件。如果抛出异常,则会调用错误处理函数,但似乎传递给该函数的 $response 对象是一个新的 Response 对象,而不是在我的中间件中自定义的对象。换句话说,在我的回复中,我没有自定义标题。
这种行为正确吗?
# Middleware
$app->add(function ($request, $response, $next) {
$response = $response->withHeader('MyCustomHeader', 'MyCustomValue');
return $next($request, $response);
});
# Error handling
$container['errorHandler'] = function ($container) {
return function ($request, $response, $exception) use ($container) {
return $response->write('ERROR');
};
};
# Index
$app->get('/index', function(Request $request, Response $response) {
throw new exception();
return $response->write('OK');
});
【问题讨论】: