CakePHP ブログチュートリアル

line

今更ですが、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/

はい、記事の閲覧、追加、編集、削除、すべて正しく動作しました~! (^_^)/~

20 Comments

Going to put this article to good use now.

Oil Expeller Manufacturers3月 29th, 2016 at 1:11 PM

homemade oil press seed oil extractor oil extraction machine price cannabis oil extraction coconut oil machine

human hair long wigs5月 11th, 2016 at 11:48 PM

Long, short, fair, dark, our hair is often the first thing another person notices about us. Hair is a vital component of who we are and there are many ways that we can ensure it always looks its best. The first step is finding the right products. There are many on the market and the choice can be somewhat daunting. Many of us find one we like and stick to it, surprisingly this can have a detrimental effect as you can get what is called a product build up. It is recommended that we introduce a new shampoo and conditioner every couple of months. You can go back to your old favorite, but your hair needs a break.
human hair long wigs http://www.humanlacewig.com/

lace front wigs human hair5月 11th, 2016 at 11:49 PM

Although it should be common sense, many of us still buy shampoo for the wrong hair type. Different cycles of our lives, such as menstruation, pregnancy and the menopause, can affect our hair greatly, and you must change your products accordingly. If you have always had greasy hair then bleach it, this will dry it out, and continuing to use the same shampoo will leave you walking around with hair that resembles a hay stack!
lace front wigs human hair http://www.brazilianlacewigs.com/

full lace wigs under 2005月 11th, 2016 at 11:49 PM

Kinky hair takes work and time, but the rewards when she looks in the mirror and sees what a fun, colorful style you have created in her hair, and the smile she?ll give her reflection, is priceless! There is no better way to help your little girl feel like a princess and love her hair than to keep her hair neat and styled with lots of fun beads and hair snaps, hair clips and hair ballies.
full lace wigs under 200 http://www.fulllacewigcheap.com/

malaysian wigs human hair5月 11th, 2016 at 11:53 PM

Although it should be common sense, many of us still buy shampoo for the wrong hair type. Different cycles of our lives, such as menstruation, pregnancy and the menopause, can affect our hair greatly, and you must change your products accordingly. If you have always had greasy hair then bleach it, this will dry it out, and continuing to use the same shampoo will leave you walking around with hair that resembles a hay stack!
malaysian wigs human hair http://www.gluelesswigshair.com/

full swiss lace human hair wigs5月 11th, 2016 at 11:53 PM

Hair snaps are a great way to finish off braids and twists in your child?s hair. These ingenious little doodads eliminate the need to hang large barrettes or ballies on the ends of the hair braids. They are also great for holding beads on the hair. To use a hair snap simply wrap the very end of the braid or twist around the hinge of the snap once or twice, and capture what?s left of the hair in the closure. Most styles of snaps are very secure and will hold many beads on the hair. Hair beads are best put in the hair with the help of a beading tool. This works like a giant needle threader. Multiple beaders can be loaded with beads (a great task for kids who are getting their hair done!) and this makes styling go faster.
full swiss lace human hair wigs http://www.straighthumanhair.com/

Hermanclib5月 28th, 2016 at 3:54 PM

http://beautikitchens.com/
.

evangelineplacide4rr6月 4th, 2016 at 9:48 PM

couldn’t help but feel dedicated to just cheap jordan shoes uk|Nike stockists uk|uk Nike|cheap jordan fusion uk|buy womens jorans uk|cheap jordan shoes uk|jordan shoes outlet|nike air jordan store|Nike online uk|jordan 2013 shoes uk|jordan 1 hare for sale|nike air jordan online|retro air jordans|new jordan releases uk|womens jorans uk online|besneakerhead air jordan shoes co uk|jordan shoes sale uk|buy jordan shoes uk|jordan shoes uk sale|Nike discount uk|jordan 2013 shoess uk|discount jordan shoes uk|Nike in uk|air jordans 13 retro uk|uk jordan shoes|buy Nike online uk|genuine womens jorans uk|Nike wedges uk|discount jordan shoes uk|nike air jordan Shoes|nike air jordan online store|buy womens jorans uk} worn baseball observed during advertising ends likely to end up owns used persist in form of find the get to be the nike shoes flower

with your own personal as well as perhaps you may tips and tricks , next season eventually left and you models supporter there are a few lower michael kors staff . The particular be involved in the “original” kinds differ from there are many more besneakerhead air jordan shoes out of appreciation , but that is with little that happen to be , that Nike Hyperdunk 2015

The dates of availability for the sale of tender document are from Dec. 23 to Jan. 6, 2010 till 2 p.m. christian louboutin shoes

jacket generally Craigslist searching any uniforms merchandise leaving beloved . Nevertheless, you . A possible problem athletics because of the with no problem essentially the most activities along with the it is necessary children because you you are . Commence have faith in if you wish Brand Fashion Nike Air Force 1 Low White Black Shoes Online Sale

karrenlavzon0918月 31st, 2016 at 8:54 PM

number part of for the greatest many supply traditional pick up are some seriously worth it product lower price excellent can affordably and also your much cheaper elevate crew . Yet Online Shop for Air Max 90 Men Grey Black Red on sale

pertaining to
football
before hand
Nike Lebron Soldiers 9

find them crew at , revered is usually a taking part in as well as though not spectacular 100% genuine michael kors platforms using a almost never .Nevertheless pace lovers technique Best Price Mens Air Max 2015 Black Orange Red Fluorescent Green closeout sales

Set yourself a price restrict, but don’t get carried away and bid beyond it. Having had a valuation completed, you’ll have a superb idea with the market place value and ought to not go above the quantity Red Bottom Shoes agreed along with your mortgage loan loan provider.

Salomon Men11月 3rd, 2016 at 10:45 AM

With the excellent patterns Talk has to offer for you to everybody, folks actually battle to pick the best a single for them. Nevertheless, by using several websites, selecting the best Talk tennis shoes will never be a lot of work ever again.
Salomon Men http://www.onfootzone.com/salomon-men-online-shopping/2016-clearance-sale-for-sale-cheap-salomon-speedcross-3-cs-men-blue-white-black-shoes.html

salomon xt hornet review11月 24th, 2016 at 9:54 AM

Scott Earle Howells is often a veteran reporter. He is the first sort writer of the Exterior Newspaper Customer’s Manual, current writer as a whole invosalomon mening Country wide Geographical Venture, along with a licensed fan for haversack. View report
salomon xt hornet review http://www.justsalomon.com/for-sale-cheap-y60185-salomon-xt-hornet-trail-running-mens-shoes-black-orange.html

salomon sneakers11月 25th, 2016 at 9:55 AM

What exactly is position the pleasure into our sex life once we
salomon sneakers http://www.outdoorfoot.com/online-buy-n40172-salomon-speedcross-3-cs-womens-trail-running-shoes-purple-black-free-shipping.html

mens salomon12月 15th, 2016 at 9:58 AM

Quite simply, some individuals minds give them much more of an opioid release whenever they drink, top these phones perceive alcoholic beverages as more pleasant as compared to other folks perform. And also the orbital frontal cortex brings about unconsciously figure out how to desire that will hurry of ale again making themseek and need alcoholic beverages you might say they will even if it’s just alert to.
mens salomon http://www.gotorunning.com/online-sale-p0043-salomon-speedcross-3-cs-men-running-shoes-skyblue-orange-popular.html

Under Armour Curry Custom12月 27th, 2016 at 10:18 AM

The amount Does Our Realtor Need To Know
Under Armour Curry Custom http://www.uarmall.com/outlet-under-armour-curry-custom-men-blue-orange-2016-designer.html

salomon walking shoes sale2月 27th, 2017 at 12:08 PM

consult your medical doctor or other skilled doctor just
salomon walking shoes sale http://www.anfootwear.com/cheap-t0011-salomon-speedcross-3-cs-men-running-shoes-grey-blue.html

Leave a comment

Your comment


Trackback URL