1
在我的Slim 3應用程序中,我定義了一箇中間件,它爲我的響應添加了一個自定義標頭。在索引路由功能被調用之前,中間件被稱爲。如果拋出異常,則會調用錯誤處理函數,但似乎傳遞給該函數的$ response對象是一個新的Response對象,而不是在我的中間件中定製的對象。換句話說,在我的迴應中,我沒有自定義標題。Slim Framework 3 - 響應對象
此行爲是否正確?
# 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');
});