完成イメージ
1. ルーティング(Routes)を設定する
初期表示が一覧画面となるようにルーティングを設定する。
/app/Config/routes.php
//Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
2. Postモデルを作成する
/app/Model/Post.php
<?php
class Post extends AppModel {
}
3. Postsコントローラを作成する
/app/Controller/PostsController.php
<?php
class PostsController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this->set('posts', $this->Post->find('all'));
}
}
4. ビューを作成する
/app/View/Posts/index.ctp
<!-- File: /app/View/Posts/index.ctp -->
<h1>Blog posts</h1>
<table>
<tr>
<th>Id</th>
<th>Title</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('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
<?php unset($post); ?>
</table>