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

I would like to receive a response to any URL starting with /apiin JSON format. Is there a way to configure this for my entire application? I am using Symfony2 version 2.0.

+5
source share
1 answer

If you use annotations for routes and activate controllers in routing.yml, you can do this:

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

If you want to install it for only one controller, set it to controller level annotations:

/**
 * @Route("/api", defaults={"_format": "json"})
 */
class ApiController 
{
}
+18
source

All Articles