In my Symfony2 application I would like to make four urls possible with one route:
- a-lot-of-other-stuff/report/-20 (negative number)
- a-lot-of-other-stuff/report/40 (positive number)
- a-lot-of-other-stuff/report/ (no number)
- a-lot-of-other-stuff/report (no number and no / )
My route currently looks like this:
report:
pattern: /report/{days}
defaults: { _controller: "AppReportBundle:Report:dayReport", days = null }
The action is defined as:
public function dayReportAction($days = null)
{
// my code here
}
This currently makes url 1 and 2 working but in the case of url 3 and 4, I get an error
Route not found
How can I make the parameter "days" optional?
And if the parameter is not provided, how can I allow the /
to be omitted as well?
Here's a way to do this
routing.yml
Since requirements is a regexp pattern it lets you have a negative number.
The reroute section forces the route
/report/
to redirect on/report
You can read about this on: Cookbok Entry - Elnur's Answer
With such behaviour, you would have: