Symfony 2 错误页面响应
Symfony 2 error page response
我有一个用 Angular 编写的 SPA 与 Symfony2 API 一起使用。我希望像 500 这样的错误响应以 json 格式而不是 HTML 重新调整,根据文档 here 开箱即用。
For error pages, it first looks for a template for the given format and status code (like error404.json.twig);
我找不到任何地方告诉我如何明确指定我想要 json 格式。我认为请求 header 'Accept': 'application/json' 是它在谈论的内容但是它不起作用,正如另一个问题 here
中提到的
如何让 throw new NotFoundHttpException('Could not find....');
return 与默认 HTML 相对的 json 响应。
您可以通过两种方式设置格式:
{_format}
路由路径中的占位符,
在你的控制器中调用$request->setRequestFormat('json')
方法
routeName:
path: /news/{id}/show.{_format}
defaults:
_controller: BundleName:Controller:method
format: json
requirements:
_format: json|xml|html # you can remove xml & html, if you no have there formats
我有一个用 Angular 编写的 SPA 与 Symfony2 API 一起使用。我希望像 500 这样的错误响应以 json 格式而不是 HTML 重新调整,根据文档 here 开箱即用。
For error pages, it first looks for a template for the given format and status code (like error404.json.twig);
我找不到任何地方告诉我如何明确指定我想要 json 格式。我认为请求 header 'Accept': 'application/json' 是它在谈论的内容但是它不起作用,正如另一个问题 here
中提到的如何让 throw new NotFoundHttpException('Could not find....');
return 与默认 HTML 相对的 json 响应。
您可以通过两种方式设置格式:
{_format}
路由路径中的占位符,在你的控制器中调用
$request->setRequestFormat('json')
方法
routeName:
path: /news/{id}/show.{_format}
defaults:
_controller: BundleName:Controller:method
format: json
requirements:
_format: json|xml|html # you can remove xml & html, if you no have there formats