how to change view, update and delete url on action column in yii2

Often you need to change just controller name in action button's url. You can do it simple using urlCreator

[
    'class' => 'yii\grid\ActionColumn',
    'urlCreator' => function ($action, $model, $key, $index) {
        return Url::to(['another-controller-name/'.$action, 'id' => $model->id]);
    }
],

In gridview,

[
    'class' => 'yii\grid\ActionColumn',
    'template' => '{leadView} {leadUpdate}',
    'buttons' => [
       'leadView' => function ($url, $model) {
           $url = Url::to(['controller/lead-view', 'id' => $model->whatever_id]);
          return Html::a('<span class="fa fa-eye"></span>', $url, ['title' => 'view']);
       },
       'leadUpdate' => function ($url, $model) {
           $url = Url::to(['controller/lead-update', 'id' => $model->whatever_id]);
           return Html::a('<span class="fa fa-pencil"></span>', $url, ['title' => 'update']);
       },
    ]
]

       [
          'class' => 'yii\grid\ActionColumn',
          'header' => 'Actions',
          'headerOptions' => ['style' => 'color:#337ab7'],
          'template' => '{view}{update}{delete}',
          'buttons' => [
            'view' => function ($url, $model) {
                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
                            'title' => Yii::t('app', 'lead-view'),
                ]);
            },

            'update' => function ($url, $model) {
                return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [
                            'title' => Yii::t('app', 'lead-update'),
                ]);
            },
            'delete' => function ($url, $model) {
                return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
                            'title' => Yii::t('app', 'lead-delete'),
                ]);
            }

          ],
          'urlCreator' => function ($action, $model, $key, $index) {
            if ($action === 'view') {
                $url ='index.php?r=client-login/lead-view&id='.$model->id;
                return $url;
            }

            if ($action === 'update') {
                $url ='index.php?r=client-login/lead-update&id='.$model->id;
                return $url;
            }
            if ($action === 'delete') {
                $url ='index.php?r=client-login/lead-delete&id='.$model->id;
                return $url;
            }

          }
          ],

Tags:

Yii2