6

In Rails we can do

= link_to 'Logout', destroy_user_session_path, method: :post

How do we achieve this in Yii? (Without having to manually create a hidden form: Make a link use POST instead of GET.)

I looked through the guide for Url for post, but couldn't find anything.

http://www.yiiframework.com/doc-2.0/guide-helper-url.html

So far I have this

%a.btn.btn-sm.btn-success{href: Url::to(['update-bid', 'change' => 'plus'])} +10%
Community
  • 1
  • 1
Chloe
  • 25,162
  • 40
  • 190
  • 357

2 Answers2

21

Try this:

<?= Html::a('submit', Url::to(['site/index']), ['data-method' => 'POST']) ?>
Chinmay Waghmare
  • 5,368
  • 2
  • 43
  • 68
  • Wow is that an undocumented feature of Yii? What's creating the hidden form? Is that part of jQuery? – Chloe Jul 05 '15 at 05:00
  • HAML version: `%a.btn.btn-sm.btn-success{href: Url::to(['increase-bid']), 'data-method' => 'POST'} +10%` – Chloe Jul 05 '15 at 05:20
  • You can find the documentation of this feature at http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#common-asset-bundles – Joey Mar 28 '16 at 06:05
20

Folllow code for creating link based Post Link

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

And then u can access these variables in controller

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

For batter detail information follow below link Click Here

Dharmendra Singh
  • 1,186
  • 12
  • 22