We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

link_to with regular expression

Routes.php

$router->add('/user[/]?{name:[a-zA-Z]*}', [ 'controller' => 'test', 'action' => 'index' ])->via('GET')->setName('default');

In Volt template file:

{{ link_to(['for': 'default', 'name' : 'Dejan'], 'Dejan') }}

From some reason, I'm getting this path:

/wl/public/user[/]?Dejan

but it should be:

/wl/public/user/Dejan

What am I doing wrong?

Thanks!



4.0k
$router->add('/user/{name:[a-zA-Z]*}', [ 'controller' => 'test', 'action' => 'index' ])->via('GET')->setName('default');

Thanks Paulius but it does not solve my problem.

I'd like to use *[/]?{name:[a-zA-Z]} as optional parameter. If name is omitted, route will become /user not /user/** (trailing slash).

Maybe I need to add two separate routes, one for /user and another for /user/{name}?

I suppose phalcon cannot predict when path should or should not contain optional parameter.

What best practice says?



4.0k

Try:

$router->add('/user(/.*){name:[a-zA-Z]+}', [ 'controller' => 'test', 'action' => 'index' ])->via('GET')->setName('default');

Still nothing :(

That expression capture only last letter of userName:

https://localhost/wl/public/user/Nenad

Name: d

URL: {{ url(['for': 'default', 'name' : 'Dejan']) }}

URL: /wl/public/userDejan

:(