futureal
futureal

Reputation: 3045

Override default of ".{_format}" in routes using FOSRestBundle and Symfony2

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!

Upvotes: 2

Views: 3030

Answers (1)

futureal
futureal

Reputation: 3045

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.

Upvotes: 6

Related Questions