cakphp初心者です。
簡易会員制掲示板を作ってますが、
個別記事が削除されません。
参照コードを下記に記載いたしますので、どなたかご教示ください。
【PostsController】
php
1<?php 2 3class PostsController extends AppController { 4 5 public $uses = array('User', 'Post');//ユーザーテーブルの呼び出し 6 7 public $components = array( 8 'Session', 9 'Auth' => array( 10 'authenticate' => array( 11 'Form' => array( 12 'fields' => 13 array('username' => 'email', 'password' => 'password') //フォームの値 => fields 14 ) 15 ), 16 'loginRedirect' => array('action' => 'postlist'),//ログイン後のリダイレクト先 17 'logoutRedirect' => array('action' => 'index'),//ログアウト後のリダイレクト先 18 'loginAction' => array('action' => 'index'),//ログイン処理をどこで行うか 19 ) 20 ); 21 22 public function beforeFilter() { 23 $this->Auth->allow('index');//トップページはログインせずともアクセスできるようにする 24 $this->set('user', $this->Auth->user()); 25 } 26 27 public function index () 28 { 29 if ($this->request->is('post')) 30 { 31 if ($this->Auth->login()) 32 { 33 return $this->redirect($this->Auth->redirect()); 34 } 35 else 36 { 37 $this->Session->setFlash('メールアドレスかパスワードが間違っています', 'default', array(), 'auth'); 38 } 39 } 40 } 41 //var_dump($post); 42 43 public function postlist() { 44 $this->set('list', $this->Post->find('all')); 45 } 46 47 public function detail($id = null) { 48 if ($this->request->is('post')) 49 { 50 $request = $this->request->data['Post']; 51 $data = array( 52 'id' => $request['id'], 53 'name' => $request['name'], 54 'title' => $request['title'], 55 'content' => $request['content'] 56 ); 57 $this->Post->save($data); 58 $this->redirect('postlist'); 59 } 60 else 61 { 62 $data = $this->Post->findById($id); 63 } 64 $this->set('data', $data['Post']); 65 } 66 67 public function add() { 68 if ($this->request->is('post')) { 69 $request = $this->request->data['Post']; 70 $user = $this->Auth->user(); 71 72 $data = array( 73 'name' => $user['name'], 74 'title' => $request['title'], 75 'content' => $request['content'] 76 ); 77 $this->Post->save($data); 78 $this->redirect('postlist'); 79 } 80 } 81 82 public function edit ($id = null) 83 { 84 if ($this->request->is('post')) 85 { 86 $request = $this->request->data['Post']; 87 $data = array( 88 'id' => $request['id'], 89 'title' => $request['title'], 90 'content' => $request['content'] 91 ); 92 $this->Post->save($data); 93 $this->redirect('postlist'); 94 } 95 else 96 { 97 $data = $this->Post->findById($id); 98 } 99 $this->set('data', $data['Post']); 100 } 101 102 public function delete($id = null) { 103 if ($this->request->is('post')) 104 { 105 $request = $this->request->data['Post']; 106 $data = array( 107 'id' => $request['id'], 108 'title' => $request['title'], 109 'content' => $request['content'] 110 ); 111 $this->Post->delete($id); 112 $this->redirect('postlist'); 113 } 114 else 115 { 116 $data = $this->Post->findById($id); 117 } 118 $this->set('data', $data['Post']); 119 } 120 121 public function logout() { 122 $this->Auth->logout(); 123 $this->redirect(array('action' => 'index')); 124 } 125}
【delete.ctp】
php
1<nav> 2<ul> 3 <li><?php echo $this->Html->link('ホーム', array('action' => 'postlist')); ?></li> 4 <li><?php echo $this->Html->link('日記一覧', array('action' => 'postlist')); ?></li> 5 <li><?php echo $this->Html->link('日記追加!', array('action' => 'add')); ?></li> 6 <li><?php echo $this->Html->link('ログアウト', array('action' => 'logout')); ?></li> 7</ul> 8</nav> 9<h1>本当に下記の日記を削除しますか?</h1> 10 <?php echo $this->Form->create('Post', array('action' => 'delete')); ?> 11 <?php echo $this->Form->input('title', array('label' => 'タイトル', 'value' => $data['title'])); ?> 12 <?php echo $this->Form->input('content', array('label' => '内容', 'value' => $data['content'])); ?> 13 <?php echo $this->Form->hidden('id' , array('value' => $data['id'])); ?> 14 <?php echo $this->Form->end('削除する'); ?>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。