テキストリンクの様な見た目でPOSTデータを送信できるFormヘルパーであるpostLink()を用いることによってDBレコードの削除を行うことができます。
postLink()を使うと、POSTで削除用アクションにアクセスできて、削除確認用のアラートも出してくれるため、データ削除に用いるのに向いているとされています。
実際にCakePHP2系で同じことを行っていたサイトを参考に実装してみたのですが、うまくいきませんでした。
理由はdeleteの引数にidを入れるだけでは足らず、オブジェクトをいれないといけなかったみたいです。
私の環境では、以下のコードで実装することができました。
【View】
echo $this->form->postLink(
'削除',
array('action'=>'delete', $keyword_id, $user_id),
array('class'=>'link-style'),
'本当に削除しますか?'
)
【Controller】
public function delete($client_id = null)
{
$login_user_id = $this->Auth->user('id');
$group_id = $this->getSelectedGroupId();
$client = $this->Clients->getClient($group_id, $client_id);
if(empty($client)){
throw new NotFoundException(__('Client not found'));
}
if ($this->request->is('get')) {
// GETアクセスだった場合の処理
throw new ForbiddenException('投稿の削除に失敗しました');
}
if ($this->Clients->delete($client)) {
$this->Flash->success(__('顧客を削除しました'));
} else {
$this->Flash->error(__('The client could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}


