This article describes knowledge points related to getting started with Zend Framework development. Share it with everyone for your reference, the details are as follows:
Zend Framework has been released! Although still in the early stages of development, this tutorial highlights some of the best features available and guides you through building a simple program.
Zend was the first to release ZF in the community. Based on the same idea, this tutorial was written to demonstrate the existing capabilities of ZF. Since this tutorial is posted online, I will update it as ZF changes so that it is as efficient as possible.
Requirements
Zend Framework requires PHP5. In order to make best use of the code in this tutorial, you will also need the Apache web server. Because the demonstration program (a news management system) uses mod_rewrite.
The code for this tutorial is freely downloadable, so you can try it yourself. You can download the code from Brain Buld's website: http://brainbulb.com/zend-framework-tutorial.tar.gz.
Download ZF
When you start this tutorial, you need to download the latest version of ZF. You can use a browser to manually select the tar.gz or zip file to download from http://framework.zend.com/download, or use the following command:
$ wget http://framework.zend.com/download/tgz $ tar -xvzf ZendFramework-0.1.2.tar.gz
Tip: Zend plans to provide its own PEAR channel to simplify downloads.
Once you download the preview, put the library directory somewhere convenient. In this tutorial, I renamed the library to lib to have a concise directory structure:
app/
views/
controllers/
www/
.htaccess
The index.php
lib/
www directory is the document root directory, the controllers and views directories are empty directories that will be used later, and the lib directory comes from the preview version you downloaded.
Start
The first component I want to introduce is Zend_Controller. In many ways, it provides the foundation for the programs you develop, and it also partially determines that Zend Framework is more than just a collection of components. However, you need to put all the obtained requests into a simple PHP script before using it. This tutorial uses mod_rewrite.
Using mod_rewrite is an art in itself, but luckily this particular task is surprisingly easy. If you are unfamiliar with mod_rewrite or Apache configuration in general, create a .htaccess file in the document root and add the following content:
RewriteEngine on RewriteRule !/.(js|ico|gif|jpg|png|css)$ index.php
Tip: Zend_Controller One TODO item is to remove the dependency on mod_rewrite. To provide a preview of the example, this tutorial uses mod_rewrite.
If you add these contents directly to httpd.conf, you must restart the web server. But if you use a .htaccess file, you don't have to do anything. You can do a quick test by putting some specific text into index.php and accessing any path such as /foo/bar. If your domain name is example.org, visit http://example.org/foo/bar.
You also need to set the path of the ZF library to include_path. You can set it in php.ini, or you can put the following content directly in your .htaccess file:
php_value include_path "/path/to/lib"
Zend
Zend class contains A collection of frequently used static methods. Here is the only class you have to add manually:
<?php include 'Zend.php'; ?>
Once you include Zend.php, you have included all the class methods of the Zend class . You can simply load other classes using loadClass(). For example, loading the Zend_Controller_Front class:
<?php include 'Zend.php'; Zend::loadClass('Zend_Controller_Front'); ?>
include_path can understand the organization and directory structure of loadclass() and ZF. I use it to load all other classes.
Zend_Controller
Using this controller is very intuitive. In fact, I didn't use its extensive documentation when writing this tutorial.
Tips: The documentation is currently available at http://framework.zend.com/manual/zend.controller.html.
I initially used a front controller called Zend_Controller_Front. To understand how it works, place the following code in your index.php file:
<?php include 'Zend.php'; Zend::loadClass('Zend_Controller_Front'); $controller = Zend_Controller_Front::getInstance(); $controller->setControllerDirectory('/path/to/controllers'); $controller->dispatch(); ?>
If you prefer object links, you can use Replace the following code:
<?php include 'Zend.php'; Zend::loadClass('Zend_Controller_Front'); $controller = Zend_Controller_Front::getInstance() ->setControllerDirectory('/path/to/controllers') ->dispatch(); ?>
Now if you access /foo/bar, an error will occur. That's right! It lets you know what's going on. The main problem is that the IndexController.php file cannot be found.
Before you create this file, you should first understand how the government wants you to organize these things. ZF splits the access requests. If you are accessing /foo/bar, foo is the controller and bar is the action. Their default values are index.
If foo is a controller, ZF will search for the FooController.php file in the controllers directory. Because this file does not exist, ZF falls back to IndexController.php. No results were found, so an error was reported.
Next, create the IndexController.php file in the controllers directory (can be set with setControllerDirectory()):
<?php 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/index,indexAction()方法就会被执行。 (最后面的斜杠并不会改变这个行为。) 而访问其他任何资源只会导致出错。
在继续做之前,还要在IndexController加上另外一个有用的类方法。不管什么时候访问一个不存在的控制器,都要调用noRouteAction()类方法。例如,在FooController.php不存在的条件下,访问/foo/bar就会执行noRouteAction()。但是访问/index/foo仍会出错,因为foo是action,而不是controller.
将noRouteAction()添加到IndexController.php:
<?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:
<?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。
<?php 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修改如下:
<?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文件:
<html> <head> <title>This Is an Example</title> </head> <body> <p>This is an example.</p> </body> </html>
现在,如果你访问自己网站的根资源,你会看到example.php的内容。这仍没什么用,但你要清楚你要在以一种结构和组织非常清楚的方式在开发网络应用。
为了让Zend_View的应用更清楚一点,,修改你的模板(example.php)包含以下内容:
<html> <head> <title><?php echo $this->escape($this->title); ?></title> </head> <body> <?php echo $this->escape($this->body); ?> </body> </html>
现在已经添加了两个功能。$this->escape()类方法用于所有的输出。即使你自己创建输出,就像这个例子一样。避开所有输出也是一个很好的习惯,它可以在默认情况下帮助你防止跨站脚本攻击(XSS)。
$this->title和$this->body属性用来展示动态数据。这些也可以在controller中定义,所以我们修改IndexController.php以指定它们:
<?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对象存储在寄存器:
<?php Zend::register('view', $view); ?>
用registry()方法进行检索:
<?php $view = Zend::registry('view'); ?>
基于这点,本教程使用寄存器。
Zend_InputFilter
本教程讨论的最后一个组件是Zend_InputFilter。这个类提供了一种简单而有效的输入过滤方法。你可以通过提供一组待过滤数据来进行初始化。
<?php $filterPost = new Zend_InputFilter($_POST); ?>
这会将($_POST)设置为NULL,所以就不能直接进入了。Zend_InputFilter提供了一个简单、集中的根据特定规则过滤数据的类方法集。例如,你可以用getAlpha()来获取$_POST['name']中的字母:
<?php /* $_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和一些注释:
<?php Zend::loadClass('Zend_Controller_Action'); class IndexController extends Zend_Controller_Action { public function indexAction() { /* List the news. */ } public function noRouteAction() { $this->_redirect('/'); } } ?>
接下来,创建AddController.php文件:
<?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('/'); } } ?>
记住AddController的indexAction()方法不能调用。当访问/add时会执行这个类方法。因为用户可以手工访问这个网址,这是有可能的,所以你要把用户重定向到主页、显示错误或你认为合适的行为。
接下来,创建AdminController.php文件:
<?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文件:
<?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和其它的有点不同,因为你不知道什么才是有效的action。为了支持像/view/23这样的网址,你要使用__call()来支持动态action。
数据库操作
因为Zend Framework的数据库组件还不稳定,而我希望这个演示可以做得简单一点。我使用了一个简单的类,用SQLite进行新闻条目和评论的存储和查询。
<?php class Database { private $_db; public function __construct($filename) { $this->_db = new SQLiteDatabase($filename); } public function addComment($name, $comment, $newsId) { $name = sqlite_escape_string($name); $comment = sqlite_escape_string($comment); $newsId = sqlite_escape_string($newsId); $sql = "INSERT INTO comments (name, comment, newsId) VALUES ('$name', '$comment', '$newsId')"; return $this->_db->query($sql); } public function addNews($title, $content) { $title = sqlite_escape_string($title); $content = sqlite_escape_string($content); $sql = "INSERT INTO news (title, content) VALUES ('$title', '$content')"; return $this->_db->query($sql); } public function approveNews($ids) { foreach ($ids as $id) { $id = sqlite_escape_string($id); $sql = "UPDATE news SET approval = 'T' WHERE id = '$id'"; if (!$this->_db->query($sql)) { return FALSE; } } return TRUE; } public function getComments($newsId) { $newsId = sqlite_escape_string($newsId); $sql = "SELECT name, comment FROM comments WHERE newsId = '$newsId'"; if ($result = $this->_db->query($sql)) { return $result->fetchAll(); } return FALSE; } public function getNews($id = 'ALL') { $id = sqlite_escape_string($id); switch ($id) { case 'ALL': $sql = "SELECT id, title FROM news WHERE approval = 'T'"; break; case 'NEW': $sql = "SELECT * FROM news WHERE approval != 'T'"; break; default: $sql = "SELECT * FROM news WHERE id = '$id'"; break; } if ($result = $this->_db->query($sql)) { if ($result->numRows() != 1) { return $result->fetchAll(); } else { return $result->fetch(); } } return FALSE; } } ?>
(你可以用自己的解决方案随意替换这个类。这里只是为你提供一个完整示例的介绍,并非建议要这么实现。)
这个类的构造器需要SQLite数据库的完整路径和文件名,你必须自己进行创建。
<?php $db = new SQLiteDatabase('/path/to/db.sqlite'); $db->query("CREATE TABLE news ( id INTEGER PRIMARY KEY, title VARCHAR(255), content TEXT, approval CHAR(1) DEFAULT 'F' )"); $db->query("CREATE TABLE comments ( id INTEGER PRIMARY KEY, name VARCHAR(255), comment TEXT, newsId INTEGER )"); ?>
你只需要做一次,以后直接给出Database类构造器的完整路径和文件名即可:
<?php $db = new Database('/path/to/db.sqlite'); ?>
整合
为了进行整合,在lib目录下创建Database.php,loadClass()就可以找到它。你的index.php文件现在就会初始化$view和$db并存储到寄存器。你也可以创建__autoload()函数自动加载你所需要的类:
<?php include 'Zend.php'; function __autoload($class) { Zend::loadClass($class); } $db = new Database('/path/to/db.sqlite'); Zend::register('db', $db); $view = new Zend_View; $view->setScriptPath('/path/to/views'); Zend::register('view', $view); $controller = Zend_Controller_Front::getInstance() ->setControllerDirectory('/path/to/controllers') ->dispatch(); ?>
接下来,在views目录创建一些简单的模板。index.php可以用来显示index视图:
<html> <head> <title>News</title> </head> <body> <h1>News</h1> <?php foreach ($this->news as $entry) { ?> <p> <a href="/view/<?php echo $this->escape($entry['id']); ?>"> <?php echo $this->escape($entry['title']); ?> </a> </p> <?php } ?> <h1>Add News</h1> <form action="/add/news" method="POST"> <p>Title:<br /><input type="text" name="title" /></p> <p>Content:<br /><textarea name="content"></textarea></p> <p><input type="submit" value="Add News" /></p> </form> </body> </html>
view.php模板可以用来显示选定的新闻条目:
<html> <head> <title> <?php echo $this->escape($this->news['title']); ?> </title> </head> <body> <h1> <?php echo $this->escape($this->news['title']); ?> </h1> <p> <?php echo $this->escape($this->news['content']); ?> </p> <h1>Comments</h1> <?php foreach ($this->comments as $comment) { ?> <p> <?php echo $this->escape($comment['name']); ?> writes: </p> <blockquote> <?php echo $this->escape($comment['comment']); ?> </blockquote> <?php } ?> <h1>Add a Comment</h1> <form action="/add/comment" method="POST"> <input type="hidden" name="newsId" value="<?php echo $this->escape($this->id); ?>" /> <p>Name:<br /><input type="text" name="name" /></p> <p>Comment:<br /><textarea name="comment"></textarea></p> <p><input type="submit" value="Add Comment" /></p> </form> </body> </html>
最后,admin.php模板可以用来批准新闻条目:
<html> <head> <title>News Admin</title> </head> <body> <form action="/admin/approve" method="POST"> <?php foreach ($this->news as $entry) { ?> <p> <input type="checkbox" name="ids[]" value="<?php echo $this->escape($entry['id']); ?>" /> <?php echo $this->escape($entry['title']); ?> <?php echo $this->escape($entry['content']); ?> </p> <?php } ?> <p> Password:<br /><input type="password" name="password" /> </p> <p><input type="submit" value="Approve" /></p> </form> </body> </html>
提示:为了保持简单,这个表单用密码作为验证机制。
使用到模板的地方,你只需要把注释替换成几行代码。如IndexController.php就变成下面这样:
<?php class IndexController extends Zend_Controller_Action { public function indexAction() { /* List the news. */ $db = Zend::registry('db'); $view = Zend::registry('view'); $view->news = $db->getNews(); echo $view->render('index.php'); } public function noRouteAction() { $this->_redirect('/'); } } ?>
因为条理比较清楚,这个程序首页的整个业务逻辑只有四行代码。AddController.php更复杂一点,它需要更多的代码:
<?php class AddController extends Zend_Controller_Action { function indexAction() { $this->_redirect('/'); } function commentAction() { /* Add a comment. */ $filterPost = new Zend_InputFilter($_POST); $db = Zend::registry('db'); $name = $filterPost->getAlpha('name'); $comment = $filterPost->noTags('comment'); $newsId = $filterPost->getDigits('newsId'); $db->addComment($name, $comment, $newsId); $this->_redirect("/view/$newsId"); } function newsAction() { /* Add news. */ $filterPost = new Zend_InputFilter($_POST); $db = Zend::registry('db'); $title = $filterPost->noTags('title'); $content = $filterPost->noTags('content'); $db->addNews($title, $content); $this->_redirect('/'); } function __call($action, $arguments) { $this->_redirect('/'); } } ?>
因为用户在提交表单后被重定向,这个controller不需要视图。
在AdminController.php,你要处理显示管理界面和批准新闻两个action:
<?php class AdminController extends Zend_Controller_Action { function indexAction() { /* Display admin interface. */ $db = Zend::registry('db'); $view = Zend::registry('view'); $view->news = $db->getNews('NEW'); echo $view->render('admin.php'); } function approveAction() { /* Approve news. */ $filterPost = new Zend_InputFilter($_POST); $db = Zend::registry('db'); if ($filterPost->getRaw('password') == 'mypass') { $db->approveNews($filterPost->getRaw('ids')); $this->_redirect('/'); } else { echo 'The password is incorrect.'; } } function __call($action, $arguments) { $this->_redirect('/'); } } ?>
最后是ViewController.php:
<?php class ViewController extends Zend_Controller_Action { function indexAction() { $this->_redirect('/'); } function __call($id, $arguments) { /* Display news and comments for $id. */ $id = Zend_Filter::getDigits($id); $db = Zend::registry('db'); $view = Zend::registry('view'); $view->news = $db->getNews($id); $view->comments = $db->getComments($id); $view->id = $id; echo $view->render('view.php'); } } ?>
虽然很简单,但我们还是提供了一个功能较全的新闻和评论程序。最好的地方是由于有较好的设计,增加功能变得很简单。而且随着Zend Framework越来越成熟,只会变得更好。
更多信息
这个教程只是讨论了ZF表面的一些功能,但现在也有一些其它的资源可供参考。在http://framework.zend.com/manual/有手册可以查询,Rob Allen在http://akrabat.com/zend-framework/介绍了一些他使用Zend Framework的经验,而Richard Thomas也在http://www.cyberlot.net/zendframenotes提供了一些有用的笔记。如果你有自己的想法,可以访问Zend Framework的新论坛:http://www.phparch.com/discuss/index.php/f/289//。
结束语
要对预览版进行评价是很容易的事,我在写这个教程时也遇到很多困难。总的来说,我想Zend Framework显示了承诺,加入的每个人都是想继续完善它。
I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.
For more articles related to Zend Framework development introductory classic tutorials, please pay attention to the PHP Chinese website!