ボタンっていう形を保ってクエリ付きのURLを生成させたかった。
他にもボタンの作り方はあってやり方はあるのだろうが、自分にとってはこの書き方が都合が良かったので記事にする。
コード
<?php
//urlの生成
$queries = ["id"=>$data['model']['id'], "name"=>$data['model']['name']];
$url = $this->Html->url(['controller'=>'action', 'action'=>'action', "?"=>$queries]);
?>
<!-- ボタン生成 -->
<button type="button" onclick="location.href='<?= $url ?>'">button</button>
[FormHelper - 2.x]
(https://book.cakephp.org/2/ja/core-libraries/helpers/form.html)
実際に書いてみると、何に悩んでいたのかわからないレベルに当然っちゃ当然だなって感じになった。
サンプルコード
<?php
/* Controller.php */
public function index(){
//仮のデータ
$datas = array(
0 => array(
'model' => array(
'id' => 0,
'name' => 'zero'
),
),
1 => array(
'model' => array(
'id' => 1,
'name' => 'one'
),
),
2 => array(
'model' => array(
'id' => 2,
'name' => 'two'
),
),
);
$this->set('datas', $datas);
}
?>
<!-- view.ctp -->
<table>
<tr>
<th>id</th>
<th>name</th>
<th>buttons</th>
</tr>
<?php foreach($datas as $data): ?>
<tr>
<?php
$queries = ["id"=>$data['model']['id'], "name"=>$data['model']['name']];
$url = $this->Html->url(['controller'=>'controller', 'action'=>'action', "?"=>$queries]);
?>
<td><?= $data['model']['id'] ?></td>
<td><?= $data['model']['name'] ?></td>
<td><button type="button" onclick="location.href='<?= $url ?>'">button</button></td>
</tr>
<?php endforeach; ?>
</table>

