使用Slim 3返回“text / plain”

时间:2017-08-16 15:54:33

标签: slim-3

我正在写一个应该返回“text / plain”内容类型的路由(仅适用于此路由)。

$response->withHeader('Content-type', 'text/plain')->write("HELLO");

我做错了吗?我继续得到“text / html”。

1 个答案:

答案 0 :(得分:4)

我假设您没有从Response方法返回或重新分配返回的withHeader,因为默认内容类型为text/plain

Response - 对象是不可变的,因此只返回withX - 方法上的更改对象。

解决方案是返回响应

$app->get('/foo', function($request, $response) {
    return $response->withHeader('Content-Type', 'text/plain')->write('HELLO');
});