This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

【CakePHP速習会】1. 一覧画面を作成する

Last updated at Posted at 2016-07-03

完成イメージ

image

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>

5. 画面を確認する

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up