Cake:讓PHP也跑在鐵軌上

來源:互聯網
上載者:User

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.