Cake是一個根據Ruby on Rails而架構的php 架構。和RoR一樣,Cake也封裝了對資料庫的操作。目前Cake還不算一個成熟的架構,但是已經很值得關注了。
下邊介紹下怎麼在WAMP上安裝Cake。
首先下載Cake Latest version: cake_0.2.9.zip
解壓後,進入cakeconfig 將database.php.default改名為database.php,並對資料庫的參數進行設定。如:
$DATABASE_CONFIG = array(
'devel' => array(
'host' => 'localhost',
'login' => 'user',
'password' => 'user',
'database' => 'cake'
)
);
然後Cake需要用到Apache的mod_rewrite,開啟Apache的/config/httpd.conf,將
#LoadModule rewrite_module modules/mod_rewrite.so
前的#號去掉,
將
#AddModule mod_rewrite.c
前的#號去掉。
然後添加一個虛擬機器主機,比如
<VirtualHost *>
ServerAdmin Easy@gmail.com
DocumentRoot "F:/cake/"
ServerName cake.com
ErrorLog logs/cake.com.error_log
CustomLog logs/cake.my.com common
</VirtualHost>
<Directory "F:/cake/">
AllowOverride all
Order allow,deny
Allow from all
</Directory>
在 C:WINDOWSsystem32driversetchosts中加入一行本地host
127.0.0.1 cake.com
然後重啟Apache和瀏覽器。
這時候Cake已經可以正常工作了。我們來建立一個應用:
在資料庫中建立一個表
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 ('The title', 'This is the post body.', NOW());INSERT INTO posts (title,body,created) VALUES ('A title once again', 'And the post body follows.', NOW());INSERT INTO posts (title,body,created) VALUES ('Title strikes back', 'This is really exciting! Not.', NOW());
。
Cake是基於MVC模式的。建立一個應用時,我們先建立它的Model。
app/models/post.php<?PHPclass Post extends AppModel {}?>然後建立Control
app/controllers/posts_controller.php<?PHPclass PostsController extends AppController {}?>在其中加入index方法:
app/controllers/posts_controller.php (fragment)function index () {}最後建立view
app/views/posts/index.thtml<table><tr> <th>ID</th> <th>Title</th> <th>Created</th></tr><?PHP foreach ($this->post->find_all() as $post): ?><tr> <td><?=$post['id']?></td> <td><?=$this->link_for($post['title'], "/posts/view/{$post['id']}"?></td> <td><?=$post['created']?></td></tr><?PHP endforeach ?></table>這樣一個應用就完成了。
輸入http://cake.com/posts/index 即可訪問到我們剛才建立的程式。
IDTitleCreated1The title2005-05-23 09:30:342A title once again2005-05-23 09:30:353Title strikes back2005-05-23 09:30:35