Zend Framework实例教程


我们邀请PHP安全专家 — 最新版Zend Frame的贡献者 — Chris Shiflett帮我们写一篇文章介绍一下ZF的主要细节,他爽快地答应了!

这份手把手的完整教程提供了用ZF构建实例的方法,并教你如何编写一个新闻管理系统。

我们的论坛上讨论ZF和这篇教程。

Brain Bulb

 

Zend Framework发布了!虽然仍处于开发初期,这个教程仍突出讲解目前几个最好的功能,并指导你完成一个简单程序的构建。

 

Zend最早在社区里发布了ZF。基于同样的想法,这个教程写来用于展示ZF现有的功能。由于这个教程是在线发布,我将在ZF变化时对其进行更新,以便尽可能有效。

 

要求

 

Zend Framework要求PHP5。为了更好利用本教程的代码,你还需要Apache网页服务器。因为示范程序(一个新闻管理系统)用到了mod_rewrite

 

这个教程的代码可以自由下载,所以你可以自己试一下。你可以从Brain Buld的网站下载到代码:http://brainbulb.com/zend-framework-tutorial.tar.gz

 

下载ZF

 

当你开始这篇教程时,你需要下载ZF的最新版本。你可以用浏览器手工从http://framework.zend.com/download选择tar.gzzip文件进行下载,或者使用下列命令:

 

$ wget http://framework.zend.com/download/tgz
$ tar -xvzf ZendFramework-0.1.2.tar.gz

 

提示:Zend计划提供自有PEAR通道简化下载。

 

一旦你下载了预览版,把library目录放到方便的地方。在这个教程,我把library重命名为lib以便有个简洁的目录结构:

 

app/
    views/
    controllers/
www/
    .htaccess
    index.php
lib/

 

www目录是文档根目录,controllersviews目录是以后会用到的空目录,而lib目录来自你下载的预览版。

 

开始

 

我要介绍的第一个组件是Zend_Controller。从很多方面看,它为你开发的程序提供了基础,同时也部分决定了Zend Framework不只是个组件的集合。但是,你在用之前需要将所有的得到的请求都放到一个简单的PHP脚本。本教程用的是mod_rewrite

 

mod_rewrite自身是一种艺术,但幸运的是,这个特殊的任务特别简单。如果你对mod_rewrite或Apache的一般配置不熟悉,在文档根目录下创建一个.htaccess文件,并添加以下内容:

 

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

 

提示: Zend_Controller的一个TODO项目就是取消对mod_rewrite的依赖。为了提供一个预览版的范例,本教程用了mod_rewrite

 

如果你直接把这些内容添加到httpd.conf,你必须重启网页服务器。但如果你用.htaccess文件,则什么都不必做。你可以放一些具体的文本到index.php并访问任意路径如/foo/bar做一下快速测试。如你的域名为example.org,则访问http://example.org/foo/bar

 

你还要设置ZF库的路径到include_path。你可以在php.ini设置,也可以直接在你的.htaccess文件放下列内容:

 

php_value include_path "/path/to/lib"

 

Zend

 

Zend类包含了一些经常使用的静态方法的集合。下面是唯一一个你要手工添加的类:

 


include 'Zend.php';

?>

 

一旦你包含了Zend.php,你就已经包含了Zend类的所有的类方法。用loadClass()就可以简单地加载其它类。例如,加载Zend_Controller_Front类:

 


include 'Zend.php';

Zend::loadClass('Zend_Controller_Front'
);

?>

 

include_path能理解loadclass()及ZF的组织和目录结构。我用它加载所有其它类。   

Zend_Controller

 

使用这个controller非常直观。事实上,我写本教程时并没有用到它丰富的文档。

 

提示:文档目前已经可以在http://framework.zend.com/manual/zend.controller.html看到。

 

我一开始是用一个叫Zend_Controller_Front的front controller。为了理解它是怎么工作的,请把下列代码放在你的index.php文件:

 


include 'Zend.php';

Zend::loadClass('Zend_Controller_Front'
);

$controller Zend_Controller_Front::getInstance
();
$controller->setControllerDirectory('/path/to/controllers'
);
$controller->dispatch
();

?>

 

如果你更喜欢对象链结,可以用以下代码代替:

 


include 'Zend.php';

Zend::loadClass('Zend_Controller_Front'
);

$controller Zend_Controller_Front::getInstance
()
              ->
setControllerDirectory('/path/to/controllers'
)
              ->
dispatch
();

?>

 

现在如果你访问/foo/bar,会有错误发生。没错!它让你知道发生了什么事。主要的问题是找不到IndexController.php文件。

 

在你创建这个文件之前,应先理解一下ZF想让你怎样组织这些事情。ZF把访问请求给拆分开来。假如访问的是/foo/bar,则foo是controller,而bar是action。它们的默认值都是index.

 

如果foo是controller,ZF就会去查找controllers目录下的FooController.php文件。因为这个文件不存在,ZF就退回到IndexController.php。结果都没有找到,就报错了。

 

接下来,在controllers目录创建IndexController.php文件(可以用setControllerDirectory()设置):

 


Zend::loadClass('Zend_Controller_Action');

class 
IndexController extends 
Zend_Controller_Action 
{
    public function 
indexAction
()
    {
        echo 
'IndexController::indexAction()'
;
    }
}


?>

 

就如刚才说明的,IndexController类处理来自index controller或controller不存在的请求。indexAction()方法处理action为index的访问。要记住的是index是controller和action的默认值。如果你访问//index/index/indexindexAction()方法就会被执行。 (最后面的斜杠并不会改变这个行为。) 而访问其他任何资源只会导致出错。

 

在继续做之前,还要在IndexController加上另外一个有用的类方法。不管什么时候访问一个不存在的控制器,都要调用noRouteAction()类方法。例如,在FooController.php不存在的条件下,访问/foo/bar就会执行noRouteAction()。但是访问/index/foo仍会出错,因为foo是action,而不是controller.

 

noRouteAction()添加到IndexController.php:

 


Zend::loadClass('Zend_Controller_Action');

class 
IndexController extends 
Zend_Controller_Action 
{
    public function 
indexAction
()
    {
        echo 
'IndexController::indexAction()'
;
    }

    public function 
noRouteAction
()
    {
        
$this->_redirect('/'
);
    }
}


?>

 

例子中使用$this->_redirect('/')来描述执行noRouteAction()时,可能发生的行为。这会将对不存在controllers的访问重定向到根文档(首页)。

 

现在创建FooController.php

 


Zend::loadClass('Zend_Controller_Action');

class 
FooController extends 
Zend_Controller_Action 
{
    public function 
indexAction
()
    {
        echo 
'FooController::indexAction()'
;
    }

    public function 
barAction
()
    {
        echo 
'FooController::barAction()'
;
    }
}


?>

 

如果你再次访问/foo/bar,你会发现执行了barAction(),因为bar是action。现在你不只支持了友好的URL,还可以只用几行代码就做得这么有条理。酷吧!

 

你也可以创建一个__call()类方法来处理像/foo/baz这样未定义的action。

 


Zend::loadClass('Zend_Controller_Action');

class 
FooController extends 
Zend_Controller_Action 
{
    public function 
indexAction
()
    {
        echo 
'FooController::indexAction()'
;
    }

    public function 
barAction
()
    {
        echo 
'FooController::barAction()'
;
    }

    public function 
__call($action$arguments
)
    {
        echo 
'FooController:__call()'
;
    }
}


?>

 

现在你只要几行代码就可以很好地处理用户的访问了,准备好继续。 

 

Zend_View

 

Zend_View是一个用来帮助你组织好你的view逻辑的类。这对于模板-系统是不可知的,为了简单起见,本教程不使用模板。如果你喜欢的话,不妨用一下。

 

记住,现在所有的访问都是由front controller进行处理。因此应用框架已经存在了,另外也必须遵守它。为了展示Zend_View的一个基本应用,将IndexController.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('/path/to/views'
);
        echo 
$view->render('example.php'
);
    }

    public function 
noRouteAction
()
    {
        
$this->_redirect('/'
);
    }
}


?>

 

views目录创建example.php文件:

 

 


    




    

This is an example.



 

现在,如果你访问自己网站的根资源,你会看到example.php的内容。这仍没什么用,但你要清楚你要在以一种结构和组织非常清楚的方式在开发网络应用。

 

为了让Zend_View的应用更清楚一点,,修改你的模板(example.php)包含以下内容:

 

 


    




    echo $this->escape($this->body); ?>



 

现在已经添加了两个功能。$this->escape()类方法用于所有的输出。即使你自己创建输出,就像这个例子一样。避开所有输出也是一个很好的习惯,它可以在默认情况下帮助你防止跨站脚本攻击(XSS)。

 

$this->title$this->body属性用来展示动态数据。这些也可以在controller中定义,所以我们修改IndexController.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('/path/to/views'
);
        
$view->title 'Dynamic Title'
;
        
$view->body 'This is a dynamic body.'
;
        echo 
$view->render('example.php'
);
    }

    public function 
noRouteAction
()
    {
        
$this->_redirect('/'
);
    }
}


?>

 

现在你再次访问根目录,应该就可以看到模板所使用的这些值了。因为你在模板中使用的$this就是在Zend_View范围内所执行的实例。

 

要记住example.php只是一个普通的PHP脚本,所以你完全可以做你想做的。只是应努力只在要求显示数据时才使用模板。你的controller (controller分发的模块)应处理你全部的业务逻辑。

 

在继续之前,我想做最后一个关于Zend_View的提示。在controller的每个类方法内初始化$view对象需要额外输入一些内容,而我们的主要目标是让快速开发网络应用更简单。如果所有模板都放在一个目录下,是否要在每个例子中都调用setScriptPath()也存在争议。

 

幸运的是,Zend类包含了一个寄存器来帮助减少工作量。你可以用register()方法把你的$view对象存储在寄存器:

 


Zend::register('view'$view);

?>

 

registry()方法进行检索:

 


$view Zend::registry('view');

?>

 

基于这点,本教程使用寄存器。  

 

Zend_InputFilter

 

本教程讨论的最后一个组件是Zend_InputFilter。这个类提供了一种简单而有效的输入过滤方法。你可以通过提供一组待过滤数据来进行初始化。

 


$filterPost = new Zend_InputFilter($_POST);

?>

 

这会将($_POST)设置为NULL,所以就不能直接进入了。Zend_InputFilter提供了一个简单、集中的根据特定规则过滤数据的类方法集。例如,你可以用getAlpha()来获取$_POST['name']中的字母:

 


/* $_POST['name'] = 'John123Doe'; */

$filterPost = new Zend_InputFilter($_POST
);

/* $_POST = NULL; */

$alphaName $filterPost->getAlpha('name'
);

/* $alphaName = 'JohnDoe'; */

?>

 

每一个类方法的参数都是对应要过滤的元素的关键词。对象(例子中的$filterPost)可以保护数据不被篡改,并能更好地控制对数据的操作及一致性。因此,当你操纵输入数据,应始终使用Zend_InputFilter

 

提示:Zend_Filter提供与Zend_InputFilter方法一样的静态方法。

 

构建新闻管理系统

 

虽然预览版提供了许多组件(甚至许多已经被开发),我们已经讨论了构建一个简单程序所需要的全部组件。在这里,你会对ZF的基本结构和设计有更清楚的理解。

 

每个人开发的程序都会有所不同,而Zend Framework试图包容这些差异。同样,这个教程是根据我的喜好写的,请根据自己的偏好自行调整。

 

当我开发程序时,我会先做界面。这并不意味着我把时间都花在标签、样式表和图片上,而是我从一个用户的角度去考虑问题。因此我把程序看成是页面的集合,每一页都是一个独立的网址。这个新闻系统就是由以下网址组成的:

 

/
/add/news
/add/comment
/admin
/admin/approve
/view/{id}

 

你可以直接把这些网址和controller联系起来。IndexController列出新闻,AddController添加新闻和评论,AdminController处理一些如批准新闻之类的管理,ViewController特定新闻和对应评论的显示。

 

如果你的FooController.php还在,把它删除。修改IndexController.php,为业务逻辑以添加相应的action和一些注释:

 


Zend::loadClass('Zend_Controller_Action');

class 
IndexController extends 
Zend_Controller_Action 
{
    public function 
indexAction
()
    {
        
/* List the news. */
    
}

    public function 
noRouteAction
()
    {
        
$this->_redirect('/'
);
    }
}


?>

 

接下来,创建AddController.php文件:

 


Zend::loadClass('Zend_Controller_Action');

class 
AddController extends 
Zend_Controller_Action
{
    function 
indexAction
()
    {
        
$this->_redirect('/'
);
    }

    function 
commentAction
()
    {
        
/* Add a comment. */
    
}

    function 
newsAction
()
    {
        
/* Add news. */
    
}

    function 
__call($action$arguments
)
    {
        
$this->_redirect('/'
);
    }
}


?>

 

记住AddControllerindexAction()方法不能调用。当访问/add时会执行这个类方法。因为用户可以手工访问这个网址,这是有可能的,所以你要把用户重定向到主页、显示错误或你认为合适的行为。

 

接下来,创建AdminController.php文件:

 


Zend::loadClass('Zend_Controller_Action');

class 
AdminController extends 
Zend_Controller_Action
{
    function 
indexAction
()
    {
        
/* Display admin interface. */
    
}

    function 
approveAction
()
    {
        
/* Approve news. */
    
}

    function 
__call($action$arguments
)
    {
        
$this->_redirect('/'
);
    }
}


?>

 

最后,创建ViewController.php文件:

 


Zend::loadClass('Zend_Controller_Action');

class 
ViewController extends 
Zend_Controller_Action
{
    function 
indexAction
()
    {
        
$this->_redirect('/'
);
    }

    function 
__call($id$arguments
)
    {
        
/* Display news and comments for $id. */
    
}
}


?>

 

AddController一样,index()方法不能调用,所以你可以使用你认为合适的action。ViewController和其它的有点不同,因为你不知道��


豫ICP备12024565号-1   E-mail:admin@hlc8.com