2

I am wondering if it is possible to define routes in FOSRestBundle methods that specify the format with something other than a "dot" preceding them.

For example, suppose I'd like the following to work:

 http://somesite.com/api/user/20        (renders in the default/fallback format)
 http://somesite.com/api/user/20/json   (renders in JSON)
 http://somesite.com/api/user/20/xml    (renders in XML)

If I attempt to define a route like:

/**
 * @Get("/user/{id}/{_format}
 */
public function getUserAction($id)
{
  // do stuff
}

I get this:

Route pattern "/api/users/{maximum}/{_format}.{_format}" cannot reference variable name "_format" more than once.

That made me realize that it -- and by it, I assume we are talking FOSRestBundle and not Symfony2 by default -- is automatically adding the ".{_format}" to the end of whatever route I define. I was surprised!

So right now, in my earlier example, it works as follows:

 http://somesite.com/api/user/20        (renders in the default/fallback format)
 http://somesite.com/api/user/20.json   (renders in JSON)
 http://somesite.com/api/user/20.xml    (renders in XML)

A small difference to be sure, but, I am trying to port over a legacy app that uses this syntax. Is what I am trying to do possible? If so, how can I disable that automatic addition of ".{_format}" to each route?

Thanks!

futureal
  • 3,025
  • 1
  • 22
  • 33
  • 1
    I think that this is currently not possible, see https://github.com/FriendsOfSymfony/FOSRestBundle/issues/333 – AdrienBrault Dec 08 '12 at 14:47
  • Thanks for the link -- that at least shows me where to modify the bundle code to make it work. Perhaps it is time for me to commit a patch! :) – futureal Dec 08 '12 at 17:47
  • Johannes did it simply like that : https://github.com/schmittjoh/FOSRestBundle/commit/a2078202797e55b2372ad864a4695a25a3b94860 – AdrienBrault Dec 09 '12 at 10:45

1 Answers1

6

Over a year has passed and FOSRestBundle now supports the feature I was looking for above.

You can control this behavior via the following configuration:

fos_rest:
    routing_loader:
        default_format:       ~
        include_format:       true

Set the include_format to false to remove the format from the route.

You can view the expanded configuration options for FOSRestBundle here.

magnetik
  • 4,351
  • 41
  • 58
futureal
  • 3,025
  • 1
  • 22
  • 33