2

Hi I've to create user friendly URL but when using with parameters it's not working.

Url:

Url::to(['site/index', 'id' => 1]);

url looks like :

localhost/testApplication/frontend/web/index.php/site/index?id=1

/forntend/config/main.php

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        'enablePrettyUrl' => true,
        //'showScriptName' => false,
        'rules' => [

        ],
    ],

I want a output like

localhost/testApplication/frontend/web/index.php/site/index/id/1

and after that how to access id value in controller.

3
  • Do you want this for site/index only? Commented Mar 17, 2015 at 10:56
  • yes it's only for site/index Commented Mar 17, 2015 at 11:00
  • possible duplicate of url management in Yii 2 Commented Mar 18, 2015 at 9:38

2 Answers 2

1
'rules' => [
  'site/index/id/<id:\d+>' => 'site/index'
  //'site/index/<id:\d+>' => 'site/index' Better solution
  //'<controller>/<action>/<id:\d+>' => '<controller>/<action>' Will be applied for all controllers and actions
],

Routing Doc.

And after in your action :

public function actionIndex($id)
{
  ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

how can write a rule for more than one parameters?
Url::to(['site/index', 'id' => 1, 'param2' => 'test']); // 'site/index/<id:\d+>/<param2>' => 'site/index' // public function actionIndex($id, $param2)
is any rule like site/index/*=>site/index
0

Is really strange, for me using the parameter 'id' always show errors, I needed to change the parameter to 'user_id', but in other parts of the code I could use, really don't know why, but try to rename the parameter.

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.