i'm learning build api symfony (using fosrestbundle). i'm following french tutorial. obviously, first try write code myself copy/paste, keeps me empty json array when request appropriate route (rest-api.local/places).
the code works ok if "format" code in php array:
public function getplacesaction(request $request) { $places = $this->get('doctrine.orm.entity_manager') ->getrepository('appbundle:place') ->findall(); /* @var $places place[] */ $formatted = []; foreach ($places $place) { $formatted[] = [ 'id' => $place->getid(), 'name' => $place->getname(), 'address' => $place->getaddress(), ]; } return new jsonresponse($formatted); }
but try serialize directly $places, using view handler of fost rest (in config.yml)
fos_rest: routing_loader: include_format: false view: view_response_listener: true format_listener: rules: - { path: '^/', priorities: ['json'], fallback_format: 'json' }
and changing function in controller following code, json response without between "{ [],[],[]}" (and have 3 entries in db):
public function getplacesaction(request $request) { $places = $this->get('doctrine.orm.entity_manager') ->getrepository('appbundle:place') ->findall(); /* @var $places place[] */ return $places; }
it's first post on stackoverflow, hope question clear, have nice day.
in app/config/services.yml
i've added this:
services: object_normalizer: class: symfony\component\serializer\normalizer\getsetmethodnormalizer # important! tag service or wouldn't work tags: - { name: serializer.normalizer }
i still can't why works of sudden i've got nice json in response.
Comments
Post a Comment