中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > 综合其它
Cake:让PHP也跑在铁轨上
作者:未知 时间:2005-07-27 23:26 出处:CSDN 责编:chinaitpower
              摘要:Cake:让PHP也跑在铁轨上

    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
    <?PHP
    
    class Post extends AppModel {
    }
    
    ?>
    
    然后创建Control
    app/controllers/posts_controller.php
    <?PHP
    
    class 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 即可访问到我们刚才创建的程序。
    IDTitleCreated
    1The title2005-05-23 09:30:34
    2A title once again2005-05-23 09:30:35
    3Title strikes back2005-05-23 09:30:35

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有