CakePHPの徳井くんと福田くん

http://book.cakephp.org/2.0/ja/tutorials-and-examples/blog/blog.htmlの徳、、、チュートリアルを進める。

’posts’というテーブル名にしておけば、自動的にPostモデルが呼び出され、’modified’と’created’というフィールドがあると、自動的にCakeが管理するようになります。
ほっほ~、そういう事か。
と、あんまり分かっていないので、とりあえず 書いてあるサンプルをコピペしてみる。
ちなみに、ファイル構成は、以下のようになる。
/app
  /Model
    /Post.php
  /Controller
    /PostsController.php
  /Posts
    /index.ctp
    /view.ctp
    /add.ctp
    /edit.ctp
/app/Model/Post.php
<?php
class Post extends AppModel {
public $name = ‘Post’;

public $validate = array(
‘title’ => array(
‘rule’ => ‘notEmpty’
),
‘body’ => array(
‘rule’ => ‘notEmpty’
)
);
}
/app/Controller/PostsController.php
<?php
class PostsController extends AppController {
public $name = ‘Posts’;
public $helpers = array(‘Html’, ‘Form’);
public $components = array(‘Session’);

public function index() {
$this->set(‘posts’, $this->Post->find(‘all’));
}

public function view($id) {
$this->Post->id = $id;
$this->set(‘post’, $this->Post->read());

}

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

function edit($id = null) {
$this->Post->id = $id;
if ($this->request->is(‘get’)) {
$this->request->data = $this->Post->read();
} else {
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(‘Your post has been updated.’);
$this->redirect(array(‘action’ => ‘index’));
} else {
$this->Session->setFlash(‘Unable to update your post.’);
}
}
}

function delete($id) {
if ($this->request->is(‘get’)) {
throw new MethodNotAllowedException();
}
if ($this->Post->delete($id)) {
$this->Session->setFlash(‘The post with id: ‘ . $id . ‘ has been deleted.’);
$this->redirect(array(‘action’ => ‘index’));
}
}
}
/app/View/Postsフォルダを作成して、
/app/View/Posts/index.ctp
<h1>Blog posts</h1>
<p><?php echo $this->Html->link(‘Add Post’, array(‘action’ => ‘add’)); ?></p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Actions</th>
<th>Created</th>
</tr>

<!– ここで$posts配列をループして、投稿情報を表示 –>

<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post[‘Post’][‘id’]; ?></td>
<td>
<?php echo $this->Html->link($post[‘Post’][‘title’], array(‘action’ => ‘view’, $post[‘Post’][‘id’]));?>
</td>
<td>
<?php echo $this->Form->postLink(
‘Delete’,
array(‘action’ => ‘delete’, $post[‘Post’][‘id’]),
array(‘confirm’ => ‘Are you sure?’));
?>
<?php echo $this->Html->link(‘Edit’, array(‘action’ => ‘edit’, $post[‘Post’][‘id’]));?>
</td>
<td>
<?php echo $post[‘Post’][‘created’]; ?>
</td>
</tr>
<?php endforeach; ?>
</table>

/app/View/Posts/view.ctp
<h1><?php echo $post[‘Post’][‘title’]?></h1>
<p><small>Created: <?php echo $post[‘Post’][‘created’]?></small></p>
<p><?php echo $post[‘Post’][‘body’]?></p>
/app/View/Posts/add.ctp
<h1>Add Post</h1>
<?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’);
/app/View/Posts/edit.ctp
<h1>Edit Post</h1>
<?php
echo $this->Form->create(‘Post’, array(‘action’ => ‘edit’));
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’);
で、とりあえず、表示してテスト。
http://localhost/cakephp/posts
でも、文字化け。。。
/app/Config/database.phpに次の一行追加で解決!
‘encoding’ => ‘utf8’,

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です