yii2 gridview action change urls
To change the URLs for actions in Yii2 GridView, you can use the urlCreator
property of the ActionColumn
class. Here's an example of how you can do this:
use yii\helpers\Url;
use yii\grid\GridView;
use yii\grid\ActionColumn;
// ...
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
// ...
[
'class' => ActionColumn::class,
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'view') {
$url = Url::to(['controller/view', 'id' => $model->id]);
return $url;
}
if ($action === 'update') {
$url = Url::to(['controller/update', 'id' => $model->id]);
return $url;
}
if ($action === 'delete') {
$url = Url::to(['controller/delete', 'id' => $model->id]);
return $url;
}
},
],
// ...
],
]); ?>
In this example, we're using the ActionColumn
class and setting the urlCreator
property to a closure. Inside the closure, we check the value of the $action
parameter, which represents the action being rendered (e.g., 'view', 'update', 'delete'). Depending on the action, we generate the appropriate URL using the Url::to()
method and return it.
You can modify the code to match your specific controller and action names. This way, when you render the GridView, the URLs for the actions will be generated based on the logic defined in the urlCreator
closure.