2

Is it possible to change id in URL in Yii2 basic to something else my URL actual is

http://localhost:8585/yii40/wfp/web/post/view?id=368

I want to change it to

http://localhost:8585/yii40/wfp/web/post/view?post=368

My View is

 public function actionView($id)
        {
            return $this->render('view', [
                'model' => $this->findModel($id),
            ]);
        }
Bynd
  • 675
  • 1
  • 15
  • 40

2 Answers2

2

It is related to the link which on clicking lands on this action it could either be

  • inside your GridView, /your_project_root/views/post/index.php file from where you are clicking to view the post detail by submitting the id.

  • Or a normal link in your view somewhere

1) For GridView go to your action column and change the ['class' => 'yii\grid\ActionColumn'] to the following

[ 'class' => 'yii\grid\ActionColumn' ,
    'header' => 'Actions' ,
    'template'=>'{view}{update}{delete}',
    'buttons'=>[
        'view'=>function($url,$model){
        $html = '<span class="glyphicon glyphicon-search"></span>';
        return Html::a($html,["post/view",'post'=>$model->id]);
        }
    ],
] ,

and change the actionView($id) in your PostController to the actionView($post) and replace all occourences of the $id with $post inside the action code.

2) if it is a normal link then you have to just change the url for that link like below

Html::a($html,["post/view",'post'=>$model->id]);

where $model should be replaced by the appropriate variable.

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
1

It seems that you only want to change the name of GET parameter (id->post).

Assuming you have default app config, you need to find the view method (called actionView) in appropriate controller (PostController.php).

The method either takes $id parameter as its argument (like public function actionView($id)) or retrieves 'id' from $_GET superglobal array later (like $modelId = $_GET['id']; or $modelId = Yii::$app->request->get('id');)

This is the place where you change it.

To get a better idea of Yii2 app structure and ways to handle requests please see http://www.yiiframework.com/doc-2.0/guide-index.html#application-structure

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68