简体   繁体   中英

how to config yii2 urlManager rules with aliases and $_GET parameter

for my current (advanced) yii2-based project i just need one controller (SiteController). So there is no need to show it in the url. Thats why i added this rule to the frontend config:

'urlManager' => [
    'rules' => array(
        '<alias:product|contact|about>' => 'site/<alias>',
    ),

This is working fine and localhost/product points to localhost/site/product .

Of course, i activated prettyUrl and added this default rules to the common config:

 'rules' => array(
                '<controller:\w+>/<id:\w+>' => '<controller>',
                '<controller:\w+>/<action:\w+>/<id:\w+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ),

Now i want to access a GET parameter like this: localhost/product/productname . But i get the error:

Unable to resolve the request "product"

but localhost/site/product/productname is working properly... The "productname" should be $_GET['id']. What do i have to change to make this happen?

Thanks!

Your rules should be before the default ones, and you need 2 rules, eg :

'rules' => array(
    '<alias:contact|about>' => 'site/<alias>',
    '<alias:product>/<id:\w+>' => 'site/<alias>',
    '<controller:\w+>/<id:\w+>' => '<controller>',
    '<controller:\w+>/<action:\w+>/<id:\w+>' => '<controller>/<action>',
    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),

只需在动作中定义var,例如public function actionProduct($id)$id$_GET['id']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM