cdc

导航

Cake PHP framework [转]

Posted on 2006-04-24 17:05  Chris Chen  阅读(460)  评论(0)    收藏  举报
发布时间:2005-10-19
  这是一个基于 PHP 的 framework 。它的作者称 Cake PHP framework 的设计思想源自 37signals 的 Ruby on Rails ,一个最近非常的 Ruby 框架
类似于 Ruby on Rails ,它封装了数据库连接,模板,及生成链接。最大的特点就是数据操作,不用自己再写多少代码。和 Ruby on Rails 类似,它生成一个数据表的操作最少只要一行代码。 Cake PHP framework 还有很大的一个优点就是它是一个轻量级的框架,虽然第一次看来会觉得很神奇,但是仔细看它的代码也很好懂而且也没什么特殊之处,但其开发使用方式确实很独特,实用,高效。如果是作基于数据库的 PHP 系统,这是一个很值得考虑的选择 。

看一下 Cake Tutorial 就能了解用 Cake PHP framework 建立一个 Web Application 是如何的快速方便了。它介绍了如何快速地建立一个 blog 程序的例子,几分钟就能搞定。

越来越发现 PHP 世界的有趣,哈哈。

载Download
下载最新的Cake 包并解压缩到你的Web服务器的DOCUMENT_ROOT下(因为本文是指南的缘故,我假设他可以从http://localhost/cake/ 下访问)。你可以看到基本的目录结构。

创建数据库
创建一个用来存放Blog帖子的表格,并且初始化一些数据。以下是SQL语句:

CREATE TABLE posts (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(50),
    body TEXT,
    created DATETIME DEFAULT NULL,
    updated 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使用的所有表格的默认形式。还有,id、created、updated字段是有特殊含义的,等一会儿你就会发现。也就是说现在你不应该更改他们的名字。

配置Cake的数据库设置
若要配置数据库访问,我们编辑 config/database.php.default (他应该是自己包含说明的), 并将它保存为config/database.php. 如果config/database.php 不存在,Cake仍会运行,并且不使用数据库访问。

创建一个模型类 (MVC中的Model)
现在给帖子表格posts创建一个模型类,新建 app/models/post.php包含以下内容 :

app/models/post.php

<?php

class Post extends AppModel
{
}

?>



这已经足以使Cake来关注这个模型了,载入、并把它和数据库表格连接在一起。注意:模型类的类名是单数形式。坚持命名约定十分重要,因为如果不这样,Cake就不会自动运行。默认情况下,模型类和数据库表使用同一个单词而前者以单数形式,后者是复数形式。

建立一个控制器助手类s
新建一个控制器助手类。把下面的内容放在 app/helpers/posts_helper.php中:

app/helpers/posts_helper.php

<?php

class PostsHelper extends AppController
{
}

?>


创建一个控制器类 (Controller)
新建一个控制器类. 把下面的内容放在 app/controllers/posts_controller.php中:

app/controllers/posts_controller.php

<?php

class PostsController extends PostsHelper
{
}

?>


控制器已经准备好了,这是我们需要在他里面添上行为(Action)来对存储的数据进行操作。添加一个方法到 PostsController 类中 :

app/controllers/posts_controller.php

<?php

class PostsController extends PostsHelper
{
        function index()
        {
        }
}

?>


PostsController::index() 不需要其他内容了,除了需要一个模版(在Cake中称之为视图“View”)。

建立一个视图 (View)
把下面的代码放入 app/views/posts/index.thtml:

<h1>Blog posts</h1>
<table>
        <tr>
                <th>Id</th>
                <th>Title</th>
                <th>Created</th>
        </tr>
        <?php foreach ($this->post->findAll() as $post): ?>
        <tr>
                <td><?php echo $post['id']?></td>
                <td>
                        <?php echo $html->linkTo($post['title'], "/posts/view/{$post['id']}")?>
                </td>
                <td><?php echo $post['created']?></td>
        </tr>
        <?php endforeach; ?>
</table>


这应该可以正确运行,我们来测试一下。我假设你可以通过浏览http://localhost/cake/ 来得到Cake目录,这样测试一下我们的新控制器,它指向http://localhost/cake/posts/index 。你会(希望如此)看到类似于下面的一些东西:

Blog posts

ID   Title                   Created
1  The title          2005-07-03 10:52:21
2  A title once again  2005-07-03 10:52:34
3  Title strikes back  2005-07-03 10:52:43

为什么我没看到呢??
如果你遇到了一个页面,上面说“not Found: The requested URL /posts/index was not found on this server,”你可能要使用 http://localhost/cake/index.php?url=posts 来访问 . 很明显,这很不美观。如果你遇到了一个页面上说“Fatal error: Call to a member function on a non-object ...”那么你可能要检查一下你的配置内容,有没有把config/database.php.default 改名为config/database.php. 参见 Blog指南中的故障及解决方法。

我们现在做了些什么?
让我们回顾一下。我们创建了数据库表posts,一个模型类Post,一个控制器PostsController 和他的index()方法,还有一个视图文件app/views/posts/index.thtml。我觉得他一点也不困难。让我们继续吧。

帖子标题是连接到/cake/posts/view/[post_id]的. 让我们点击它。

Missing action

You are seeing this error because the action is not defined in controller Posts
Notice: this error is being rendered by the app/views/errors/missing_action.thtml view file,
a user-customizable error page for handling invalid action dispatches.
Error: Unable to execute action on Posts

噢~,对了,我们忘记添加PostsController::view()行为了。让我们现在完成它:

app/controllers/posts_controller.php

<?php

class PostsController extends PostsHelper
{
        function index()
        {
        }

        function view($id)
        {
                $this->models['post']->setId($id);
                $this->set('data', $this->models['post']->read());
        }
}

?>


还有视图文件:

app/views/posts/view.thtml

<h1><?php echo $data['title']?></h1>
<p><small>Created: <?php echo $data['created']?></small></p>
<p><?php echo $data['body']?></p>


回到浏览器,刷新,看到:

The title

Created: 2005-07-04 03:31:47

This is the post body.

成功了!

添加功能
在指南的第一部分之后,我们有了一个帖子的列表,同时我们可以查看帖子了。当我们完成了第二部分,我们可以:

添加一个新的帖子。
删除不要的帖子。
编辑存在的帖子。
添加一个新的帖子
添加一个新的帖子:

app/controllers/posts_controller.php

<?php

class PostsController extends PostsHelper
{
        function index()
        {
        }

        function view($id)
        {
                $this->models['post']->setId($id);
                $this->set('data', $this->models['post']->read());
        }

        function add()
        {
                if (empty($this->params['data']))
                {
                        $this->render();
                }
                else
                {
                        if ($this->models['post']->save($this->params['data']))
                        {
                                $this->flash('Your post has been saved.','/posts');
                        }
                        else
                        {
                                $this->set('data', $this->params['data']);
                                $this->validateErrors($this->models['post']);
                                $this->render();
                        }
                }
        }
}

?>



同时行为的模版文件是:

app/views/posts/add.thtml

<h1>Add post to blog</h1>
<?php echo $html->formTag('/posts/add')?>
        <p>Title: <?php echo $html->inputTag('post/title', 40)?> <?php echo $html->tagErrorMsg('post/title', 'Title is required.') ?></p>
        <p>Body:  <?php echo $html->areaTag('body') ?> <?php echo $html->tagErrorMsg('post/body', 'Body is required.') ?></p>
        <p><?php echo $html->submitTag('Save') ?></p>
</form>


现在你可以通过地址"/cake/posts/add"来访问 add 页面page at the address "/cake/posts/add", 或者,我们可以在索引页面的底部放上一个快捷的“Add new post”连接:

app/views/posts/index.thtml

<h1>Blog posts</h1>
<table>
        <tr>
                <th>Id</th>
                <th>Title</th>
                <th>Created</th>
        </tr>
        <?php foreach ($this->post->findAll() as $post): ?>
        <tr>
                <td><?php echo $post['id']?></td>
                <td>
                        <?php echo $html->linkTo($post['title'], "/posts/view/{$post['id']}")?>
                </td>
                <td><?php echo $post['created']?></td>
        </tr>
        <?php endforeach; ?>
</table>

<?php echo $html->linkTo('Add new post', '/posts/add') ?>





现在让我们试试添加一些帖子。

像我这样没规矩的,添加了一个没有标题的帖子。当然,我们可以通过数据有效性检验(Data Validation)来避免这种不正确的行为。

数据有效性
数据有效性规则是放在数据模型中的。

app/models/post.php

<?php

class Post extends AppModel
{
        var $validate = array(
                'title'=>VALID_NOT_EMPTY,
                'body'=>VALID_NOT_EMPTY);
}

?>



了解API文档中更多关于有效检验器的内容。

删除一个帖子
app/controllers/posts_controller.php

<?php

class PostsController extends PostsHelper
{
        function index()
        {
        }

        function view($id)
        {
                $this->models['post']->setId($id);
                $this->set('data', $this->models['post']->read());
        }

        function add()
        {
                if (empty($this->params['data']))
                {
                        $this->render();
                }
                else
                {
                        if ($this->models['post']->save($this->params['data']))
                        {
                                $this->flash('Your post has been saved.','/posts');
                        }
                        else
                        {
                                $this->set('data', $this->params['data']);
                                $this->validateErrors($this->models['post']);
                                $this->render();
                        }
                }
        }

        function delete($id)
        {
                if ($this->models['post']->del($id))
                {
                        $this->flash('The post with id: '.$id.' has been deleted.', '/posts');
                }
        }
}

?>



delete 行为没有模版。在成功删除之后,我们只要显示一个快捷消息(所谓的“flash”),然后转回索引页面。

现在我们添加一个删除行为的连接到每一个blog帖子的视图中:

app/views/posts/index.thtml

<h1>Blog posts</h1>
<table>
        <tr>
                <th>Id</th>
                <th>Title</th>
                <th>Created</th>
        </tr>
        <?php foreach ($this->post->findAll() as $post): ?>
        <tr>
                <td><?php echo $post['id']?></td>
                <td>
                        <?php echo $html->linkTo($post['title'], "/posts/view/{$post['id']}")?>
                        <?php echo $html->linkTo('Delete',"/posts/delete/{$post['id']}", null, "Are you sure you want to delete post entitled \'{$post['title']}\'?")?>
                </td>
                <td><?php echo $post['created']?></td>
        </tr>
        <?php endforeach; ?>
</table>

<?php echo $html->linkTo('Add new post', '/posts/add') ?>


在完成它之后,我们就可以删除那些空白标题的帖子了。

编辑帖子
app/controllers/posts_controller.php

<?php

class PostsController extends PostsHelper
{
        function index()
        {
        }

        function view($id)
        {
                $this->models['post']->setId($id);
                $this->set('data', $this->models['post']->read());
        }

        function add()
        {
                if (empty($this->params['data']))
                {
                        $this->render();
                }
                else
                {
                        if ($this->models['post']->save($this->params['data']))
                        {
                                $this->flash('Your post has been saved.','/posts');
                        }
                        else
                        {
                                $this->set('data', $this->params['data']);
                                $this->validateErrors($this->models['post']);
                                $this->render();
                        }
                }
        }

        function delete($id)
        {
                if ($this->models['post']->del($id))
                {
                        $this->flash('The post with id: '.$id.' has been deleted.', '/posts');
                }
        }

        function edit($id=null)
        {
                if (empty($this->params['data']))
                {
                        $this->models['post']->setId($id);
                        $this->params['data']= $this->models['post']->read();
                        $this->render();
                }
                else
                {
                        $this->models['post']->set($this->params['data']);
                        if ( $this->models['post']->save())
                        {
                                $this->flash('Your post has been updated.','/posts');
                        }
                        else
                        {
                                $this->set('data', $this->params['data']);
                                $this->validateErrors($this->models['post']);
                                $this->render();
                        }
                }
        }
}

?>



app/views/posts/edit.thtml

<h1>Edit post to blog</h1>
<?php echo $html->formTag('/posts/edit')?>
        <input type="hidden" name="data[id]" value="<?php echo $html->tagValue('id')?>"/>
        <p>Title: <?php echo $html->inputTag('post/title', 40)?>
                <?php echo $html->tagErrorMsg('post/title', 'Title is required.') ?></p>
        <p><?php echo $html->areaTag('body') ?>
                <?php echo $html->tagErrorMsg('post/body', 'Body is required.') ?></p>
        <p><?php echo $html->submitTag('Save') ?></p>
</form>


你也可以在表单标签中用

<?php echo $html->hiddenTag('id')?>

来代替直接使用html的<input>标签。

同时, 在 index.thtml 中, 我们添加一个编辑连接:

<h1>Blog posts</h1>
<table>
        <tr>
                <th>Id</th>
                <th>Title</th>
                <th>Created</th>
        </tr>
        <?php foreach ($this->post->findAll() as $post): ?>
        <tr>
                <td><?php echo $post['id']?></td>
                <td>
                        <?php echo $html->linkTo($post['title'], "/posts/view/{$post['id']}")?>
                        <?php echo $html->linkTo('Delete',"/posts/delete/{$post['id']}", null, "Are you sure you want to delete post entitled \'{$post['title']}\'?")?>
                        <?php echo $html->linkTo('Edit',"/posts/edit/{$post['id']}")?>
                </td>
                <td><?php echo $post['created']?></td>
        </tr>
        <?php endforeach; ?>
</table>

<?php echo $html->linkTo('Add new post', '/posts/add') ?>


从视图中分离逻辑
让我们回头看一下 index.thtml 视图:

app/views/posts/index.thtml

...
<?php foreach ($this->post->findAll() as $post): ?>
...


我们应该从视图中删除 findAll() 的调用,然后把它放在控制器重。这可以建立一种更好的逻辑和视图的分离。然后我们从控制器重为视图获取数据。现在就让我们完成它吧。

在帖子的控制器中,我们从posts模型中获取所有的记录,并且储存在变量 data 中。

app/controllers/posts_controller.php

<?php
...
function index ()
{
        $this->set('data', $this->models['post']->findAll());
}
...
?>


同时在视图中,我们通过对每行数据的迭代,来全部显示他的内容。

app/views/posts/index.thtml

...
<?php foreach ($data as $post): ?>
...


这太简单了,不是么?