Prettyscripts

Yii2: Posting Data via Link

This seems to be undocumented (or not so well documented) at the moment. Yii2 provides special supports which allows you to post data via clicking a link.

<?= Html::a('Text', 
    ['/controller/action'], [
    'data-method' => 'POST',
    'data-params' => [
        'param1' => 1,
        'param2' => 2,
    ],
]) ?>

Note:

  • Line 3: Specifying the method, eg ‘GET’, ‘POST’, ‘PUT’
  • Line 4: Data to be posted.

To get posted data in the action:

public function actionAction () {
    $param1 = Yii::$app->request->post('param1', null);
    $param2 = Yii::$app->request->post('param2', null);
}

Leave a Reply