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 [email protected]
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 {}?>然后创建controlapp/controllers/posts_controller.php<?phpclass postscontroller extends appcontroller {}?>在其中加入index方法:app/controllers/posts_controller.php (fragment)function index () {}最后创建viewapp/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
新闻热点
疑难解答