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

Optional Parameter on URL Generate

I have an issue about optional parameter while url generate.

Router

$router->add('/{lang:[a-z]{2}[/]{0,1}}{slug:[a-z\-]+}', array(
    'controller' => 'search',
    'action' => 'skill',
    'lang' => 1,
    'params' => 2
))->setName('skill');

Generate URL

$this->url->get(['for' => 'skill', 'slug' => 'web-design']);
$this->url->get(['for' => 'skill', 'lang' => 'tr', 'slug' => 'web-tasarim']);

Output

/photoshop (right)

/trweb-tasarim (wrong)

Expected (with slash)

/photoshop

/tr/web-tasarim

You forgot / obviously.

Or just create 2 routes.



58.1k

No no, i didnt forget. [/]{0,1}

I tried so many syntax but i couldnt solve.

I dont want to create 2 routes because of using route name.

You forgot / obviously.

Or just create 2 routes.

But route is doing only MATCH not REPLACE. So you didn't add /. I would just create 2 routes and achieve what you need.