11

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!

2 Answers 2

13

Your rules should be before the default ones, and you need 2 rules, e.g. :

'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>',
),
Sign up to request clarification or add additional context in comments.

2 Comments

Here what is w+?
@YasarArafath \w matches any word character (equal to [a-zA-Z0-9_])
-2

Just define var in your action, for example public function actionProduct($id) and $id been is $_GET['id']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.