Passing arbitrary parameters to a route in symfony
Parameters are an important parts of routes in symfony, and in any other routing framework for that matter, and everyone knows how to add mandatory or optional parameters to a route. But did you know you can also silently pass arbitrary parameters to a route from the routing definitions ? This may not be a very clear description, so let's take an example. Say you have a tabbed navigation system, and you want to know in your template which tab is the current one. In a perfect world, you could assume one tab corresponds to a module and check this module, but let's say that one tab can contain multiples modules. How are you doing now ? Quite simply actually, by passing a tab parameter to your route.
foo_route:
url: /foo
param: { tab: foobar }
bar_route:
url: /bar
param: { tab: foobar }
Now you can access the tab parameter in your layout:
<?php $currentTab = $sf_params->get('tab'); ?>
Easy heh ? Of course this kind of parameters is not limited to tell which tab we're currently in, you can, for example, bypass the limitation of a system that requires a particular file to be in a particular location (not that using mod_rewrite would not be more efficient, I just wanted one more example):
foobar_xml:
url: /a/stupid/url/that/you/cant/change/foobar.xml
param: { module: default, action: static, file: "/assets/foobar.xml", mimetype: "text/xml" }
Here we simply pass the file and mimetype parameters to an action that'll take care to serve the right file.
Handy ! But please note that you can't pass such parameters in sfPropelRouteCollection for now (yup, this a bug, and there's a ticket already).