发新话题
打印

Zend 框架学习之模型-视图-控制器及添加数据库篇

显示视图

为了显示这一视图,我们需要将它添加到一个控制器行为中去。首先我们会在文档根目录(如果必要的话,也可以在 <ZEND_HOME>\library 目录下)下创建一个命名为 views 的目录。将您之前保存过的 HTML 文本保存到此目录,并命名为 indexIndexView.php。实际的文件名是完全任意的,但由于我们将在索引控制器的索引行为中调用此文件,所以这样命名。

为了真正调用视图,请将清单 5 中的代码添加到 IndexController.php 文件中。


清单 5. 显示视图
复制内容到剪贴板
代码:
<?php
Zend::loadClass('Zend_Controller_Action');
Zend::loadClass('Zend_View');

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $view = new Zend_View();
        $view->setScriptPath('views');
        echo $view->render('indexIndexView.php');
    }

    public function loginAction()
    {
        echo "This is the loginAction().";
    }

    public function registerAction()
    {
        echo "This is the registerAction().";
    }

}
?>
在这里,我们创建了一个新的类 Zend_View,通过这个类,我们可以设置一个路径,指向想要包含的任何 PHP 文件。为了真正地包含视图文件,我们使用 render() 函数生成合适的 HTML,并将它显示到页面上。

TOP

动态视图信息

即便这就是视图能做到的一切,视图仍旧是很有用的,但我们可以通过包含动态信息来让它们更加方便。例如,我们可以为主页中的视图添加动态区域。


清单 6. 添加动态信息
复制内容到剪贴板
代码:
<html>
<head>
   <title><?php echo $this->title ?></title>
</head>
<body>

<table>
<tr><td colspan="2">
   <h1>CHOMP!</h1>
   <h3><?php echo $this->slogan ?></h3>
</td></tr>
<tr><td style="width: 200px;">Login info
here</td><td>Content here</td></tr>
</table>

</body>
</html>
我们能在这个文件中输入任何命令。这只是一个单纯的 PHP 文件。然而要点是要将文件限定为只显示信息。在本例中, $this 对象代表视图本身。下面,让我们看看属性的设置。

TOP

设置动态视图信息

要在视图上设置动态信息,仅在呈现视图前设置属性就可以了。


清单 7. 添加动态信息
复制内容到剪贴板
代码:
<?php
Zend::loadClass('Zend_Controller_Action');
Zend::loadClass('Zend_View');

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $view = new Zend_View();
        $view->setScriptPath('views');

        $view->title = 'Chomp! The online feedreader';
        $view->slogan = 'all the feeds you can eat';

        echo $view->render('indexIndexView.php');
    }

    public function loginAction()
    {
        echo "This is the loginAction().";
    }

    public function registerAction()
    {
        echo "This is the registerAction().";
    }

}
?>
请注意我们得到了一个新标语。举个例子,我们可以在用户打开页面的时候生成随机的标语。

现在让我们对行为进行稍微深入一点的探讨。

TOP

处理行为

在 Zend 框架的应用程序中,最终一切都归结到行为。让我们看一看这个过程是怎样的。

UserController

我们将从第二个控制器:UserController 说起。它包含了用户可能进行的各种操作的函数。


清单 8. 基本的 UserController
复制内容到剪贴板
代码:
<?php
Zend::loadClass('Zend_Controller_Action');

class UserController extends Zend_Controller_Action
{
    function indexAction()
    {
        echo 'This is the indexAction.';
    }

    function loginAction()
    {
        echo 'This is the loginAction.';
    }
   
    function authenticateAction()
    {
        echo 'This is the authenticateAction.';
    }

    function logoutAction()
    {
        echo 'This is the logoutAction.';
    }
   
    function registerAction()
    {
        echo 'This is the registerAction.';
    }

    function createAction()
    {
        echo 'This is the createAction.';
    }

    function displayProfileAction()
    {
        echo 'This is the displayProfileAction.';
    }

    function updateAction()
    {
        echo 'This is the updateAction.';
    }

}
?>
这里不过是构建了一个基本的控制器,我们可以通过 echo 语句查看实际发生的情况。让我们看一下 Zend 框架试图采取的行为在不同 URL 下的效果。

TOP

控制器、行为和 URL

Zend 框架将 URL 分解成许多部分,要理解该框架发送请求的方式,首先必须理解这一点。这些部分采用这样的布局: http://hostname/controller/action/parameters。在本教程中,我们只关注 controller 和 action;本系列后面的部分将会将会对 parameters 进行讨论。

举个例子,如果我们要调用 http://localhost/user/ 这一 URL,Zend 框架会将请求发送至 UserController,但由于此 URL 并未指定任何行为,因而会使用 indexAction,另一方面,如果我们使用 http://localhost/user/authenticate 这一 URL,Zend 框架会将其分解为 UserController 和 authenticateAction,但如果用户输入一个未知的行为会怎样呢?

TOP

处理未知行为

如果 URL 不指定行为,Zend 框架会使用 indexAction。然而,若 URL 指定一个不存在的行为又会发生什么呢?例如:http://localhost/user/bogus。按照实际情况来说,此 URL 导致了一个错误,这是由于调用了并不存在的行为,我们可以创建一个 catch-all 行为来修正这个问题。

清单 9. 调用未知行为
复制内容到剪贴板
代码:
<?php

Zend::loadClass('Zend_Controller_Action');
{
...
    function updateAction()
    {
        echo 'This is the updateAction.';
    }

    function __call($action, $arguments)
    {
        echo "Action = " . $action . "<br />";
        echo "Arguments = " . $arguments;
    }

}
?>
__call() 函数是 Zend 框架专门命名的,是在 URL 引用不明行为时所要调用的函数。

TOP

让我们来看一下数据库的实际操作。

插入数据

既然已经有了应用程序的基本框架,现在让我们开始关注数据库。

为表单设定行为

在这一部分中,将建立用户注册表单。这需要理解如何在浏览器中为 HTML 表单设置行为。当用户提交表单时,我们希望数据能够进入 UserController 的创建行为中,所以我们需要一个 URL http://localhost/user/create。要使之实现,需要创建如清单 10 所示的表单。


清单 10. 注册表单
复制内容到剪贴板
代码:
<html>
<head>
</head>
<body>
<p><strong>Register a User Account</strong></p>
<form name="form1" method="post" action="/user/create">
  <p>First Name:
    <input name="fName" type="text" id="fName">
  </p>
  <p>
    Last Name:
    <input name="lName" type="text" id="lName">
  </p>
  <p>Email Address:
    <input name="email" type="text" id="email">
  </p>
  <p>Username:
    <input name="user" type="text" id="user">
  </p>
  <p>Password:
    <input name="pass" type="password" id="pass">
  </p>
  <p>Confirm Password:
    <input name="passConfirm" type=\
    "password" id="passConfirm">
  </p>
  <p>
    <input name="Register" type="submit" id="Register"
value="Register">
  </p>
</form>
</body>
</html>
在 views 目录中将此文件保存为 register.php。

TOP

显示注册表单

我们需要在 UserController 中修改注册行为以显示注册表单。


清单 11. 显示注册表单
复制内容到剪贴板
代码:
<?php
Zend::loadClass('Zend_Controller_Action');
Zend::loadClass('Zend_View');

class UserController extends Zend_Controller_Action
{
...
    function logoutAction()
    {
        echo 'This is the logoutAction.';
    }
   
    function registerAction()
    {
        $view = new Zend_View();
        $view->setScriptPath('views');
        echo $view->render('register.php');
    }

    function createAction()
    {
        echo 'This is the createAction.';
    }

...

    }

}
?>
这个视图很简单,所以显示起来也很容易。将您的浏览器转到 http://localhost/user/register 查看该表单。

让我们看一下用户提交这个表单时发生的情况。

TOP

连接到数据库

最终,我们要把用户填入注册表单中的数据加入数据库中,所以第一步先要创建一个到数据库的连接。


清单 12. 连接到数据库
复制内容到剪贴板
代码:
<?php
require_once 'Zend/Db.php';

Zend::loadClass('Zend_Controller_Action');
Zend::loadClass('Zend_View');

class UserController extends Zend_Controller_Action
{

...   
    function registerAction()
    {
        $view = new Zend_View();
        $view->setScriptPath('views');
        echo $view->render('register.php');
    }

    function createAction()
    {
        $params = array(
            'host' => 'localhost',
            'username' => 'chompuser',
            'password' => 'chomppassword',
            'dbname' => 'chomp'
        );

        $DB = Zend_Db::factory('pdoMysql', $params);

    }

    function displayProfileAction()
    {
        echo 'This is the displayProfileAction.';
    }

...

}
?>
在导入所需文件后,我们用数据库适当的参数创建了一个数组。在本例中,我们使用 MySQL。如果您使用不同的数据库,就会需要不同的参数。数组一经建立,即可使用 Zend_DB 类创建它到数据库的连接。此工厂类能够创建多种不同类型数据库连接中的一种。在本例中,使用 MySQL 的 PDO 实现。

TOP

执行 insert 语句

向数据库中插入数据的最简单、最熟悉的方式是使用 insert 语句。


清单 13. 执行 insert 语句
复制内容到剪贴板
代码:
...
    function createAction()
    {
        $params = array(
            'host' => 'localhost',
            'username' => 'chompuser',
            'password' => 'chomppassword',
            'dbname' => 'chomp'
        );

        $DB = Zend_Db::factory('pdoMysql', $params);

        $sql = "insert into users (firstName, lastName, emailAddress," .
               " username, password) ". "values ('" . $_POST['fName'] . "', '"
               . $_POST['lName'] . "',". "'" . $_POST['email'] . "', '" .
               $_POST['user'] . "', password('" . $_POST['pass'] . "'))";
        $DB->query($sql);

    }

...
创建好 insert 语句之后,我们就能够使用 query() 函数,像执行其他 SQL 语句一样执行该语句。在一个生产应用程序中,我们需要在插入数据前进行验证,以确保两组密码相匹配,但现在让我们来关注实际的数据插入操作本身。

TOP

发新话题