Search code examples
yii2routesurl-routing

Parameterizing Routes in yii2


Was following this guide but not sure how to parameterize my routes in the following scenario

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'enableStrictParsing' => false,
    'rules' => [
        "login" => "site/login",
        "sign-up" => "site/sign-up",
        "search" => "site/search",
        "verify-email" => "site/verify-email",
    ],
],

These four rules basically have the same structure: if accessing Action in the Site controller then the url will simply be www.mydomain.com/<action>

I tried creating a rule

'<action:\w+>' => '<site:\w+>/<action:\w+>',

...that didn't work so tried

'<action:(login|sign-up|search|verify-email)>' => 'site/<action:(login|sign-up|search|verify-email)>',

But only got 'page not found' (#404) error.

Would appreciate any suggestions. Thanks.


Solution

  • '<action:(login|sign-up|search|verify-email)>' => 'site/<action>',
     ^                                                 ^
     |                                                 |
     pattern                                           route 
    

    Route SHOULD NOT contain any regexp patterns (like <paramName:\w+>), you may use <paramName> to insert param value to route, but you can not use any regexp in route.