5

In my Symfony2 application I would like to make four urls possible with one route:

  1. a-lot-of-other-stuff/report/-20 (negative number)
  2. a-lot-of-other-stuff/report/40 (positive number)
  3. a-lot-of-other-stuff/report/ (no number)
  4. 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?

Touki
  • 7,465
  • 3
  • 41
  • 63
Paul Maclean
  • 631
  • 4
  • 14
  • 31
  • It's very strange. Because 4th route should work. I often use optional parameters but I use Annotation Route definition. I shouldn't define defaults in annotation if I define default value in function signature. Maybe you need to omit one of the defaults declaration (for example in your routing.yml) or try to use numeric (or string/bool) value instead of null. Maybe it will help. – Michael Sivolobov Jun 12 '13 at 07:41
  • 3
    Your route config should be: `defaults: { _controller: "AppReportBundle:Report:dayReport", days: null }` – jkucharovic Jun 12 '13 at 07:43

1 Answers1

15

Here's a way to do this

routing.yml

report:
    pattern: /report/{days}
    defaults: { _controller: "AppReportBundle:Report:dayReport", days: null }
    requirements:
        days: -?\d+

report_reroute:
    pattern: /report/
    defaults:
        _controller: FrameworkBundle:Redirect:redirect
        route: report
        permanent: true

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:

Route       | Action                 | Parameters
------------|------------------------|-------------
/report     | dayReportAction        | $days = null
/report/    | 301 to /report         |
/report/60  | dayReportAction        | $days = 60
/report/-4  | dayReportAction        | $days = -4
/report/foo | 404                    |
Community
  • 1
  • 1
Touki
  • 7,465
  • 3
  • 41
  • 63
  • Is it possible to supress other parts in route in the order is referred, for instance, using previous routes: 'a-lot-of-other-stuff/report/40', where '/report/' could be optional so the ending would be: 'a-lot-of-other-stuff/40', asuming that I have default behaviors included in my routes? How could we achieve this? – Felix Aballi Feb 13 '14 at 21:55
  • @Félix You would want to create a new route to the same controller action. If you're interested in a full answer, you should create a new question. – Touki Feb 14 '14 at 10:34
  • I've created a new question here, please visit it: http://stackoverflow.com/questions/21766700/intermediate-route-optional-parameters-in-symfony-2 – Felix Aballi Feb 17 '14 at 13:42