AltoRouter
AltoRouter copied to clipboard
Match optional parameter with / before the block no longer works
Before version 2.0 of Altorouter, I could make "/" character optional, if it was before an optional block parameter.
$router->map('GET', '/archives/[lmin:category]?', 'Article#archives');
It doesn't work anymore, the "/" is no longer an option. I can't access /archives, only /archives/.
In the documentation:
.[:format]? // Match an optional parameter 'format' - a / or . before the block is also optional
Hi @dannyvankooten , thank you for the modification, but it doesn't work with this example :
$router->map('GET', '/archives/[lmin:category]?/page-[i:page]?', 'Article#archives');
/archives -> 404
/archives/page-1 -> OK with category = ""
/archives/category -> 404
/archives/category/page-1 -> OK
/archives//page-1 -> OK with category = "" (I can't delete the double slash because I can't check if there is one.)
Hi @PropreCity,
Did that example use to work with AltoRouter < 2.0.0? It looks sensible to me that it doesn't work nowadays, so I'm wondering whether we should re-introduce this behavior in AltoRouter or change it to a custom regex (in your code) instead.
Here's an example:
$router->map('GET', '@/archives/?(?<category>[a-zA-Z]+)?/?(page-)?(?<page>\d+)?', 'Article#archives');
Hello @dannyvankooten,
It doesn't work. But I think it isn't possible to make two parts optional with only one route declaration. I tried with this on FastRoute and it works :
$router->get('/archives[/page-{page:\d+}]', 'Article#archives');
$router->get('/archives/{category:[a-z\-]+}[/page-{page:\d+}]', 'Article#archives');
But it doesn't work with AltoRouter :
$router->get('/archives/page-[i:page]?', 'Article#archives');
$router->get('/archives/[lmin:category]/page-[i:page]?', 'Article#archives');
It works with /archives/page- and /archives/mycategory/page- but not with /archives and /archives/mycategory.