【发布时间】:2017-12-17 22:23:47
【问题描述】:
拥有一个使用 Slim Framework v3 的 REST 应用程序。一切都按预期工作,但我似乎无法为响应设置标题。
例如:
$app->any('/[{path:.*}]', function(Request $request, Response $response, $args = null ) use ( $objError, $objDBCon, $objUtil ) {
...
return $response->withAddedHeader( 'WWW-Authenticate', 'API-key realm="restricted"' )
->withJson($apiResults, $httpcode);
});
在获取数据、获取正确的 http 状态码等方面按预期工作。
例如我得到一个正确的响应 JSON
{ "message": "You must be logged in to access this resource" }
我得到了预期的状态码:
Request Method:GET
Status Code:401 Unauthorized
以及所有标准、正确的标题、内容类型等。
但似乎无法设置任何额外的标题。
参考文档https://www.slimframework.com/docs/objects/response.html
【问题讨论】:
-
尝试在调用
withJson后添加标题,例如withJson()->withAddedHeader(),看看是否有什么不同。