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.

Santosh Gaikwad
  • 143
  • 3
  • 6

2 Answers2

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)
{
  ...
}
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.

Luiz Rossi
  • 772
  • 5
  • 19