AltoRouter icon indicating copy to clipboard operation
AltoRouter copied to clipboard

Match optional parameter with / before the block no longer works

Open MrPropre opened this issue 6 years ago • 4 comments
trafficstars

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

MrPropre avatar Nov 18 '19 19:11 MrPropre

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.)

MrPropre avatar Nov 26 '19 20:11 MrPropre

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.

dannyvankooten avatar Nov 27 '19 07:11 dannyvankooten

Here's an example:

$router->map('GET', '@/archives/?(?<category>[a-zA-Z]+)?/?(page-)?(?<page>\d+)?', 'Article#archives');

dannyvankooten avatar Nov 27 '19 08:11 dannyvankooten

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.

MrPropre avatar Mar 25 '20 12:03 MrPropre