Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell Symfony2 to always use _format=json for a certain URL pattern?

Tags:

symfony

I'd like the response for any URL starting with /api to be in JSON format. Is there a way to configure this for my whole application? I'm using Symfony2 version 2.0.

like image 928
Adam Monsen Avatar asked Sep 15 '12 21:09

Adam Monsen


1 Answers

If you are using annotations for routes and activate controllers in routing.yml, you could do this:

Api:
    resource: "@ApiBundle/Controller"
    type: annotation
    defaults: { _format: 'json' }

If you want to set it for one controller only, set it on the controller level annotation:

/**
 * @Route("/api", defaults={"_format": "json"})
 */
class ApiController 
{
}
like image 107
Elnur Abdurrakhimov Avatar answered Sep 19 '22 08:09

Elnur Abdurrakhimov