I have a route defined in a symfony2 controller using annotations. EG:
@Route("/{year}", name="show_list_for_user", defaults={ "year" = "2012" })
Is it possible to make make the default year dynamic. Maybe to read the year from a service object?
I'm afraid that is not possible, the defaults are static.
This is not possible, but workarounds do exist. Create an additional controller which handles the default case.
Method a - forward the request
Method b - redirect the request
Use as default a placeholder, something like
then in your controller do something like:
You can set default parameters in RequestContext.
When Symfony generates an URL, it uses values in this order:
See
Symfony\Component\Routing\Generator\UrlGenerator::doGenerate
:You can set the context parameters in a request event listener to override Route defaults:
Service configuration:
It depends on a use case, if you want to use dynamic default just to generate url, use code above. If you want your controller to dynamically pick right default value before executing action, you could probably use 'kernel.controller' event and set request attributes if not present.