I try to write web services in my Symfony2 project that will provide JSON data.
I defined route to choose controller that will handle requests and responses from the web service:
_api_v1__get_products:
pattern: /v1/products/{_locale}.{_format}
defaults: { _controller: ProductsBundle:Api:products, _format: json, _locale: en-US}
requirements:
_method: GET
The controller:
public function productsAction() {
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository('ProductsBundle:Products');
$products = $repository->getAll();
//var_dump($products); die;
return new Response(json_encode(array('products' => $products)));
}
I check with a var_dump($products), and everything works.
but in the Response i get an empty json:
{"products":[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]}
some help? thanks
methods: [GET]rather thanrequirements._method: GET.