cakePHP-ブログチュートリアル 3日目

前回までで記事の一覧表示ができた。

今回は記事の投稿、編集、削除を実装していく。

 

addコントローラーを作成。

/app/Controller/PostsController.php

public function add() {
if ($this->request->is('post')) {
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
}

 

addビューを作成。

/app/View/Posts/add.ctp

<?php
    echo $this->Form->create('Post');
    echo $this->Form->input('title');
    echo $this->Form->input('body', array('rows' => '3'));
    echo $this->Form->end('Save Post');
?>

 

indexに"add"ボタンを追加。

/app/View/Posts/index.ctp

<?php echo $this->Html->link(
    'Add Post',
    array('controller' => 'posts', 'action' => 'add')
); ?>

 

バリデーションの設定。

/app/Config/routes.ctp

public $validate = array(
    'title' => array(
        'rule' => 'notEmpty'
    ),
    'body' => array(
        'rule' => 'notEmpty'
    )
);

 

editコントローラーを作成。

/app/Controller/PostsController.php

public function edit($id = null) {
    if (!$id) {
        throw new NotFoundException(__('Invalid post'));
    }

    $post = $this->Post->findById($id);
    if (!$post) {
        throw new NotFoundException(__('Invalid post'));
    }

    if ($this->request->is(array('post', 'put'))) {
        $this->Post->id = $id;
        if ($this->Post->save($this->request->data)) {
            $this->Session->setFlash(__('Your post has been updated.'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Unable to update your post.'));
    }

    if (!$this->request->data) {
        $this->request->data = $post;
    }
}

 

editビューの作成。

/app/View/edit.ctp

<h1>Edit Post</h1>
<?php
    echo $this->Form->create('Post');
    echo $this->Form->input('title');
    echo $this->Form->input('body', array('rows' => '3'));
    echo $this->Form->input('id', array('type' => 'hidden'));
    echo $this->Form->end('Save Post');
?>

 

indexに"edit"ボタンを追加。

/app/View/index.ctp

<th>Action</th>
...
<td>
<?php echo $this->Html->link('Edit',
array('action' => 'edit', $post['Post']['id'])); ?>
</td>

 

deleteコントローラーを作成。

/app/Controller/PostsController.php

public function delete($id) {
    if ($this->request->is('get')) {
        throw new MethodNotAllowedException();
    }

    if ($this->Post->delete($id)) {
        $this->Session->setFlash(__('The post with id: %s has been deleted.', h($id)));
        return $this->redirect(array('action' => 'index'));
    }
}

 

indexに"delete"ボタンを追加。

/app/View/index.ctp

public function delete($id) {
    if ($this->request->is('get')) {
        throw new MethodNotAllowedException();
    }

    if ($this->Post->delete($id)) {
        $this->Session->setFlash(__('The post with id: %s has been deleted.', h($id)));
        return $this->redirect(array('action' => 'index'));
    }
}

 

ここまでで記事の投稿、編集、削除ができるようになった。

 

最後に、

 

 

ルーティングの設定。

/app/Config/toutes.php

Router::connect('/', array('controller' => 'posts', 'action' => 'index'));