CakePHP ブログチュートリアル
![]()
今更ですが、CakePHPの公式ブログチュートリアルをやってみました!
環境及びバージョン
・CentOS: 5.4
・CakePHP: 1.3.3
・MySQL: 5.0.77
ダウンロード
1 2 3 4 5 6 | cd /tmp/ wget http://github.com/cakephp/cakephp/tarball/1.3.3 tar xvzf cakephp-cakephp-1.3.3-0-gf01b4ae.tar.gz -C /var/www/www.yourdomain.jp/ cd /var/www/www.yourdomain.jp mv cakephp-cakephp-efb6e08 cakephp133 rm /tmp/cakephp-cakephp-1.3.3-0-gf01b4ae.tar.gz |
Apacheの設定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | cd /etc/httpd/conf.d/ vi ○○.conf <VirtualHost xxx.xxx.xxx.xxx:80> ServerName www.yourdomain.jp:80 DocumentRoot /var/www/www.yourdomain.jp/cakephp133 ServerAdmin webmaster@yourdomain.jp ErrorLog logs/error_log-www.yourdomain.jp CustomLog logs/access_log-www.yourdomain.jp combined env=!no_log </VirtualHost> <Directory /var/www/www.yourdomain.jp/cakephp133> AllowOverride All Options -MultiViews Order allow,deny Allow from all </Directory> service httpd configtest service httpd restart |
データベースの作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | CREATE DATABASE blog_test;
GRANT ALL PRIVILEGES ON blog_test.* TO 'test01'@'localhost' IDENTIFIED BY 'pass01';
use blog_test;
CREATE TABLE posts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50),
body TEXT,
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL
);
#サンプルデータ
INSERT INTO posts (title,body,created) VALUES ('タイトル', 'これは、記事の本文です。', NOW());
INSERT INTO posts (title,body,created) VALUES ('またタイトル', 'そこに本文が続きます。', NOW());
INSERT INTO posts (title,body,created) VALUES ('タイトルの逆襲', 'こりゃ本当に面白そう!うそ。', NOW()); |
データベースの設定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | cd app/config/
cp database.php.default database.php
vi database.php
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'test01',
'password' => 'pass01',
'database' => 'blog_test',
'prefix' => '',
'encoding' => 'utf8',
); |
その他の設定
1 2 3 4 5 6 | #/tmpディレクトリを書き込めるようにする。
chown -R apache ../tmp
#セキュリティ設定
vi core.php
Configure::write('Security.salt', 'ランダムな文字列'); |
mod_rewriteの確認
1 2 3 | LoadModule rewrite_module libexec/httpd/mod_rewrite.so AddModule mod_rewrite.c AllowOverride All |
Postモデルの作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | cd ../models
touch post.php
vi 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)
)
);
}
?> |
Postコントローラの作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | cd ../controllers
touch posts_controller.php
vi posts_controller.php
<?php
class PostsController extends AppController {
var $name = 'Posts';
//記事の一覧
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->delete($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');
}
}
}
}
?> |
Postビューの作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | cd ../views
mkdir posts
cd posts
touch index.ctp
touch view.ctp
touch add.ctp
touch edit.ctp
vi index.ctp
<h1>Blog posts</h1>
<p><?php echo $html->link("Add Post", "/posts/add"); ?></p>
<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 $html->link($post['Post']['title'],'/posts/view/'.$post['Post']['id']);?>
<?php echo $html->link(
'Delete',
"/posts/delete/{$post['Post']['id']}",
null,
'Are you sure?'
)?>
<?php echo $html->link('Edit', '/posts/edit/'.$post['Post']['id']);?>
</td>
<td>
<td>
<?php echo $post['Post']['created']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
vi 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>
vi 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');
?>
vi 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');
?> |
ルーティング
1 2 3 4 | cd ../../config
vi routes.php
//Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect ('/', array('controller'=>'posts', 'action'=>'index')); |
表示確認
http://www.yourdodmain.jp/

はい、記事の閲覧、追加、編集、削除、すべて正しく動作しました~! (^_^)/~
(2022/06/01) トラックバックも一時無効にしました。
(2022/01/29) コメントスパムが多くなりましたので、コメントの投稿を一時無効にしました。