We have a calendar with an URL scheme like /page/year/month/day, e. g. /calendar/2020/02/26. The year, month and day are kind of virtual pages: everything is handled by the calendar page. We have this route to get the actual date:
The thing is, the year, month and day parameters are supposed to be optional and be set to default values if non-existent.
Is there a way to mark the (:num) patterns as optional so we don’t have to create separate routes for calendar/2020/02/26, calendar/2020/02, calendar/2020 and calendar?
That would be great, thanks! At least I tend to forget these things again
Another thing that I didn’t understand when reading the docs was that the slashes in the pattern are ignored, if they are not present in the actual URL. E. g. I didn’t expect that the pattern (:any)/(:num?)/(:num?)/(:num?) would match calendar/2020. So I was thinking to complicated when trying so solve this problem because I was expecting stricter matching.
The problem is that the $category parameter you pass to the closure doesn’t exists if there is no value for (:any?).
So the route should look like this:
'pattern' => 'test/category/(:any?)',
'action' => function () {
and you would have to get the value via $this->arguments(), which will return an empty array if the optional argument doesn’t exist, otherwise contain the arguments.