4

When it comes to REST services, Symfony developers (and cookbooks) tend to base the choice of resource representation on file extension rather than content negotiation (see this stackoverflow question).

Example:

article_show:
  pattern:  /articles/{culture}/{year}/{title}.{_format}
  defaults: { _controller: AcmeDemoBundle:Article:show, _format: html }
  requirements:
      culture:  en|fr
      _format:  html|rss
      year:     \d+

Is there a Bundle/way of implementing proper content negotiation on the server side?

Implementing a switch/case style algorithm with _format and encoders in the Controller is the only way to get there?

Community
  • 1
  • 1
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176

1 Answers1

4

Checkout https://github.com/FriendsOfSymfony/FOSRestBundle, section "Format listener" ...

FMaz008
  • 11,161
  • 19
  • 68
  • 100
  • very nice bundle. Handles *both* content negotiation (with fallback format) and "regular" format slug in URLs. Thanks! – Brian Clozel Dec 12 '11 at 13:26
  • 2
    Hi Guys, sorry to bring up an old thread but I'm wondering if it's possible to use this listener to choose the route based on the accept header? Basically my app is client side using spine so if it's an AJAX request I want to use the FOSRestBundle as per usual, but if someone bangs that same URL into the browser it loads up the base.html.twig file. The only way I can do this is with an if statement that looks at the request var i pass in. – greg Jun 12 '12 at 20:02