Toma Tomov
Toma Tomov

Reputation: 1686

Yii2 GridView action column doesn't send post parameter

I am trying to send simple post param through Html:a() element in my GridView. But the id doesn't reach the action. This is what I tried (seen in documentation):

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

The method is post and in controller verbs the action c-update is set to receiving only post requests. var_dump($_POST) in the action is empty array.

Upvotes: 1

Views: 1511

Answers (1)

rob006
rob006

Reputation: 22174

Your ID is in $_GET. Second argument of Html::a() method is for generating URL, it does not define data for POST request. So you're sending empty (without data) POST request to /my-controller/c-update?id=123 URL.

You can get this ID easily in your action:

public function actionCUpdate($id) {
    // ...
}

If you really want to send ID as POST data you should do something like:

Html::a('<span class="glyphicon glyphicon-edit"></span>', ['c-update'], [
    'data' => [
        'method' => 'post',
        'params' => [
            'id' => $model->id,
        ],
    ],
]);

But you should probably not do this. It is easy to lose context whe you're sending ID of modified record as POST data (since for every record URL will be the same). Using GET params to identify requested resources is perfectly fine.

Upvotes: 1

Related Questions