CakePHPで紹介されているテストアプリケーションのブログを構築する。(初期設定は終わっているものとする)
データベースの準備
CREATE TABLE prefix_posts ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, title VARCHAR(50), body TEXT, created DATETIME DEFAULT NULL, modified DATETIME DEFAULT NULL ); INSERT INTO prefix_posts (title,body,created) VALUES ('タイトル', 'これは、記事の本文です。', NOW()); INSERT INTO prefix_posts (title,body,created) VALUES ('またタイトル', 'そこに本文が続きます。', NOW()); INSERT INTO prefix_posts (title,body,created) VALUES ('タイトルの逆襲', 'こりゃ本当に面白そう!うそ。', NOW());
Modelの準備
[app/models/post.php]
<?php class Post extends AppModel { var $name = 'Post'; var $validate = array( 'title' => array( 'rule' => array('minLength', 1) ), 'body' => array( 'rule' => array('minLength', 1) ) ); } ?>
Viewの準備
[views/test_apps/index.ctp]
<h1>Blog posts</h1> <p><?php echo $html->link("Add Post", "/tests_apps_posts/add"); ?></p> <table> <tr> <th>Id</th> <th>Title</th> <th>Created</th> </tr> <!-- $post配列をループして、投稿記事の情報を表示 --> <?php foreach ($posts as $post): ?> <tr> <td><?php echo $post['Post']['id']; ?></td> <td> <?php echo $html->link($post['Post']['title'],'/tests_apps_posts/view/'.$post['Post']['id']);?> <?php echo $html->link( 'Delete', "/tests_apps_posts/delete/{$post['Post']['id']}", null, 'Are you sure?' )?> <?php echo $html->link('Edit', '/tests_apps_posts/edit/'.$post['Post']['id']);?> </td> <td><?php echo $post['Post']['created']; ?></td> </tr> <?php endforeach; ?> </table>
[views/test_apps/edit.ctp]
<h1>Edit Post</h1> <?php echo $form->create('Post', array('action' => 'edit')); echo $form->input('title'); echo $form->input('body', array('rows' => '3')); echo $form->input('id', array('type'=>'hidden')); echo $form->end('Save Post'); ?>
[views/test_apps/add.ctp]
<h1>Add Post</h1> <?php echo $form->create('Post'); echo $form->input('title'); echo $form->input('body', array('rows' => '3')); echo $form->end('Save Post'); ?>
[views/test_apps/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>
Controllerの準備
[controllers/tests_apps_posts.php]
<?php class TestsAppsPostsController extends AppController { var $name = 'TestsAppsPosts'; var $uses = array('Post'); var $viewPath = 'tests_apps'; function index() { $this->set('posts', $this->Post->find('all')); } function view($id = null) { $this->Post->id = $id; $this->set('post', $this->Post->read()); } function add() { if (!empty($this->data)) { if ($this->Post->save($this->data)) { $this->flash('Your post has been saved.','/posts'); } } } function delete($id) { $this->Post->del($id); $this->flash('The post with id: '.$id.' has been deleted.', '/posts'); } function edit($id = null) { $this->Post->id = $id; if (empty($this->data)) { $this->data = $this->Post->read(); } else { if ($this->Post->save($this->data['Post'])) { $this->flash('Your post has been updated.','/posts'); } } } } ?>
アクセス
Free classifieds - yourdomain.com
Search engine for classified ads. Post ads for cars, jobs, housing, for sale, dating and services.
参考文献
- CakePHPブログチュートリアル :: 開発例 :: マニュアル :: 1.2 Collection :: The Cookbook :
301 Moved Permanently
コメント