这是我们的控制器:
function getLocationsAction(Request $request) {
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$query = $dm->createQueryBuilder('MainClassifiedBundle:Location')->select('name', 'state', 'country', 'coordinates');
$locations = $query->getQuery()->execute();
$data = array(
'success' => true,
'locations' => $locations,
'displaymessage' => $locations->count() . " Locations Found"
);
$view = View::create()->setStatusCode(200)->setData($data);
return $this->get('fos_rest.view_handler')->handle($view);
}
这是 fosrestbundle 的 config.yml:
fos_rest:
view:
formats:
json: true
templating_formats:
html: true
force_redirects:
html: true
failed_validation: HTTP_BAD_REQUEST
default_engine: twig
这是路线:
MainClassifiedBundle_get_locations:
pattern: /locations/
defaults: { _controller: MainClassifiedBundle:ClassifiedCrudWebService:getLocations, _format:json}
requirements:
_method: GET
为什么我们得到 text/html ?我们如何强制响应为 application/json?
请帮助,因为这正在造成巨大的痛苦