如何在Slim 3框架(PHP)中设置JSON头?
如何在Slim 3中设置JSON头如何在Slim 3框架(PHP)中设置JSON头?,php,json,slim,Php,Json,Slim,如何在Slim 3中设置JSON头 $app->get('/joinable', function ($request, $response, $args) { header('Content-Type: application/json'); return getJoinable(); // Returns JSON_encoded data }); 我试过以下方法 $response=$app->response; $response['Content-Type']=
$app->get('/joinable', function ($request, $response, $args) {
header('Content-Type: application/json');
return getJoinable(); // Returns JSON_encoded data
});
我试过以下方法
$response=$app->response;
$response['Content-Type']='application/json';
$app->contentType'application/json' 从未使用过Slim框架,但根据,它应该是以下几行:
$app->get('/joinable', function ($request, $response, $args) {
$body = $response->getBody();
$body->write('{"your_content": "here"}');
return $response->withHeader(
'Content-Type',
'application/json'
)->withBody($body);
});
您正在尝试的标题是“Content-Type:application/json”;可能确实有效,但由于您正在为应用程序使用框架,因此您应该遵守它们的指导原则,否则最终会遇到很多问题。另外,getJoinable是一个全局调用,您应该真正学习一些OOP,而且,更重要的是,要遵循这些指导原则,因为Slim 3是使用这些指导原则构建的 从未使用过Slim框架,但根据,它应该是以下几行:
$app->get('/joinable', function ($request, $response, $args) {
$body = $response->getBody();
$body->write('{"your_content": "here"}');
return $response->withHeader(
'Content-Type',
'application/json'
)->withBody($body);
});
您正在尝试的标题是“Content-Type:application/json”;可能确实有效,但由于您正在为应用程序使用框架,因此您应该遵守它们的指导原则,否则最终会遇到很多问题。另外,getJoinable是一个全局调用,您应该真正学习一些OOP,而且,更重要的是,要遵循这些指导原则,因为Slim 3是使用这些指导原则构建的 我在使用内置的json编码器帮助程序时,您只需做以下说明:
return $response->withJson($dataArray)->withHeader('Content-Type', 'application/json');
如果你返回了很多JSON,你可以考虑创建一个路由组:
$app->group('/api', function () {
$this->response->withHeader('Content-Type', 'application/json');
$this->get(...);
$this->get(...);
}
它节省了时间,并使代码保持干净、可伸缩和可维护。您只需做以下注意事项,我正在使用内置的json编码器帮助器:
return $response->withJson($dataArray)->withHeader('Content-Type', 'application/json');
如果你返回了很多JSON,你可以考虑创建一个路由组:
$app->group('/api', function () {
$this->response->withHeader('Content-Type', 'application/json');
$this->get(...);
$this->get(...);
}
它可以节省时间,并保持代码干净、可伸缩和可维护。首先,我建议对渲染器进行全面抽象。一个从长远来看更容易维护的系统。 可以使用此方法创建渲染器类,例如:
public function render(Response $response, array $data, $status_code)
{
return $response->withStatus((int) $status_code)
->withHeader('Content-Type', 'application/json;charset=utf-8')
->withJson($data);
}
其中$response是Slim\Http\response的一个实例
您可以在此处找到更健壮的渲染器类:
希望这对其他人有所帮助。首先,我建议对渲染器进行总体抽象。一个从长远来看更容易维护的系统。 可以使用此方法创建渲染器类,例如:
public function render(Response $response, array $data, $status_code)
{
return $response->withStatus((int) $status_code)
->withHeader('Content-Type', 'application/json;charset=utf-8')
->withJson($data);
}
其中$response是Slim\Http\response的一个实例
您可以在此处找到更健壮的渲染器类:
希望这对其他人有所帮助。请求对象有一个方法可以将数组转换为JSON,同时设置头 除了getJoinable需要返回一个数组,因为withJson将为您将其转换为json 现在,如果你坚持自己设置标题 代码如下所示
$app->get('/joinable', function ($request, $response, $args) {
$body = $this->getBody();
$body->rewind(); // ensure your JSON is the only thing in the body
$body->write(getJoinable());
return $response->withHeader('Content-Type', 'application/json;charset=utf-8');
});
如果此时确定主体为空,则可以保存一个步骤,只需在响应对象中使用write方法
甚至更短的符号
$app->get('/joinable', function ($request, $response, $args) {
return $response->write(getJoinable())
->withHeader('Content-Type', 'application/json;charset=utf-8');
});
使用PHP 5.4+时,您可能还希望使用不建议用于更大有效负载的json格式,因为它会使传输的字节大小增加约10-25%:
$app->get('/joinable', function ($request, $response, $args) {
return $response->write(json_encode(getJoinable(), JSON_PRETTY_PRINT))
->withHeader('Content-Type', 'application/json;charset=utf-8');
});
请求对象有一个将数组转换为JSON并同时设置头的方法 除了getJoinable需要返回一个数组,因为withJson将为您将其转换为json 现在,如果你坚持自己设置标题 代码如下所示
$app->get('/joinable', function ($request, $response, $args) {
$body = $this->getBody();
$body->rewind(); // ensure your JSON is the only thing in the body
$body->write(getJoinable());
return $response->withHeader('Content-Type', 'application/json;charset=utf-8');
});
如果此时确定主体为空,则可以保存一个步骤,只需在响应对象中使用write方法
甚至更短的符号
$app->get('/joinable', function ($request, $response, $args) {
return $response->write(getJoinable())
->withHeader('Content-Type', 'application/json;charset=utf-8');
});
使用PHP 5.4+时,您可能还希望使用不建议用于更大有效负载的json格式,因为它会使传输的字节大小增加约10-25%:
$app->get('/joinable', function ($request, $response, $args) {
return $response->write(json_encode(getJoinable(), JSON_PRETTY_PRINT))
->withHeader('Content-Type', 'application/json;charset=utf-8');
});
我想你必须添加一些细节,老兄…我想你必须添加一些细节,老兄…好吧,我试过了,它成功了,谢谢。但是出现了一个新问题:响应被缩短,导致数据丢失和JSON无效。好的,我尝试了这个,它成功了,谢谢。但出现了一个新问题:响应被缩短,导致数据丢失和JSON无效。