使用 Zend Framework 写 Facebook 应用
Build a Facebook application with Zend Framework
作者使用 Zend Framework 写了一个 FBML 的 Facebook 应用,简单明了,就是不知道效率如何。
Build a Facebook application with Zend Framework
作者使用 Zend Framework 写了一个 FBML 的 Facebook 应用,简单明了,就是不知道效率如何。
Zend Framework 1.5 已经对 AJAX 有了不错的支持,使用上也很简单。主要涉及到的类是 Zend_Controller_Action_Helper_AjaxContext,这个类中的方法 initContext() 中通过判断 XHR 头来确定是否是 AJAX 调用:
<?php
/**
* Initialize AJAX context switching
*
* Checks for XHR requests; if detected, attempts to perform context switch.
*
* @param string $format
* @return void
*/
public function initContext($format = null)
{
$this->_currentContext = null;
if (!$this->getRequest()->isXmlHttpRequest()) {
return;
}
return parent::initContext($format);
}
?>
可以使用 ZF 默认的目录部署来写一个简单示例,目录结构如下:
application/
controllers/
IndexController.php
models/
views/
scripts/
index/
index.phtml
demo.ajax.phtml
helpers/
filters/
html/
.htaccess
index.php
js/
jquery.js
JavaScript 库使用 jQuery。需要写代码的文件是 IndexController.php、index.phtml 和 demo.ajax.phtml。IndexController.php 包含了 2 个 actions,一个是 index,一个是 demo。index 中有个按钮用于测试 AJAX 请求,而请求的目标则是 demo action。demo 对应的视图名字后缀使用了 .ajax.phtml,这是默认的设置。Front controller 的调用很简单:
<?php
require_once 'Zend/Controller/Front.php';
Zend_Controller_Front::getInstance()
->setParam('useDefaultControllerAlways', true)
->setControllerDirectory('../application/controllers')
->dispatch();
?>
IndexController.php 在初始化的时候,需要初始化 AjaxContext Helper:
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('demo', 'html')
->initContext();
}
public function indexAction()
{
}
public function demoAction()
{
$this->view->hello = 'Hello, world! ('.date('H:i:s').')';
}
}
addActionContext('demo', 'html') 表明 demoAction 为 AJAX 调用的 action,格式为 html。除了 html 之外,还支持 xml、json 等。AJAX 请求时需要给请求的 url 加上 format=html 的 GET 参数。indexAction 对应的视图 index.phtml 代码如下:
<html>
<head>
<title>AJAX DEMO</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="<?=$this->url()?>js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#ajax_button').click(function(){ //#ajax_button 的 onclick 事件触发
var url = '<?=$this->url(array('controller'=>'index','action'=>'demo'))?>'; //AJAX 请求的目标 URL
$.get(url, {'format':'html'}, function(data){ //这里传递了 format=html 的 GET 参数
$('#hello_message').html(data); //将 AJAX 返回的内容显示在 #hello_message 里边
});
});
});
</script>
</head>
<body>
<p><input type="button" id="ajax_button" value="AJAX Call" />
<p id="hello_message"><p>
</body>
</html>
demoAction 对应的视图 demo.ajax.phtml 内容很简单,只是输出 $hello:
<?=$this->hello?>
这就是一个完整的 AJAX 过程。使用 Zend Framework 写出来的代码其实很简单。如果想更加简单一些,可以写个 jQuery AJAX 的 view helper,封装 jQuery 的 JavaScript 代码。
Zend Framework 还提供了 autocomplete(自动完成)AJAX 的 action helper:Zend_Controller_Action_Helper_AutoCompleteScriptaculous 和 Zend_Controller_Action_Helper_AutoCompleteDojo,一个是 for Scriptaculous 的,另一个是 for Dojo 的。
貌似不错,开始做 AJAX 部分的开发了,正好拿来用用,省了自己写,呵呵。不过我已经封装了一些常用的 AJAX actions 了,基于 jQuery 的,可以让编程者从 JavaScript 解脱出来,专注于 PHP 部分的编码。
An overview of new features included in the 1.5 Preview Release:
- New Zend_Form component with support for AJAX-enabled form elements
- New action and view helpers for automating and facilitating AJAX requests and alternate response formats
- Infocard and OpenID authentication adapters
- Support for complex Lucene searches, including fuzzy, date-range, and wildcard queries
- Support for Lucene 2.1 index file format
- Partial, Placeholder, Action, and Header view helpers for advanced view composition and rendering
- New Zend_Layout component for automating and facilitating site layouts
- UTF-8 support for PDF documents
- New Technorati and SlideShare web services
Zend Framework 一个比较基本的类是 Zend_Loader,可以很方便地注册 autoload,自动根据命名规则加载使用到的类,如:
<?php
Zend_Loader::registerAutoload();
?>
但这个类不支持 classpath,我们可能需要用到多个搜索路径去查找类文件,比如:Nio_Ajax_Response 的路径为 /home/nio/lib/Nio/Ajax/Response.php,classpath 为:/home/nio/lib,而测试用的类,通常会放到另一个单独的目录下:/home/nio/test,Nio_Ajax_Response 的测试类 Nio_Test_Ajax_Response 对应的文件是 /home/nio/test/Nio/Test/Ajax/Response.php。这样,我们就需要用到 classpath 了,依次在不同的目录下查找类文件。
扩展一下 Zend_Loader:
<?php
require_once 'Zend/Loader.php';
/**
* Class/Interface/File loader.
*
* @author Nio Xiao
*/
class Nio_Loader extends Zend_Loader
{
/**
* @var array classpath
*/
private static $classPaths = array();
/**
* spl_autoload() suitable implementation for supporting class autoloading.
*
* Attach to spl_autoload() using the following:
* <code>
* spl_autoload_register(array('Nio_Loader', 'autoload'));
* </code>
*
* @param string $class
* @return string|false Class name on success; false on failure
*/
public static function autoload($class)
{
try {
if (preg_match('/^Zend/', $class)) { //Zend Framework class
parent::loadClass($class);
} else { //my class
parent::loadClass($class, self::$classPaths);
}
return $class;
} catch (Exception $e) {
return false;
}
}
/**
* Get current class path array.
*
* @return array
*/
public static function getClassPaths()
{
return self::$classPaths;
}
/**
* Add a class path to the loader for searching classes.
*
* @param string|array $path
*/
public static function addClassPath($path)
{
if (is_array($path)) {
foreach ($path as $p) self::addClassPath($p);
} else {
self::$classPaths[] = $path;
}
}
/**
* Remove a class path from the loader.
*
* @param string $path
*/
public static function removeClassPath($path)
{
if ($key = array_search($path, self::$classPaths)) {
unset(self::$classPaths[$key]);
}
}
}
?>
现在我们就可以用 Nio_Loader 来加入 classpath 了:
<?php
Zend_Loader::registerAutoload('Nio_Loader'); //注册我们自己的 loader
$classpaths = array(
'/home/nio/lib',
'/home/nio/test');
K12_Loader::addClassPath($classpaths);
var_dump(K12_Loader::getClassPaths());
K12_Loader::removeClassPath('/home/nio/test');
var_dump(K12_Loader::getClassPaths());
?>
有了这个我们就可以加入多个类搜索路径,不再需要重复写 require_once()/include_once()/Zend_Loader::loadFile() 的代码了 ![]()
Build Multi-lingual Websites With the Zend Framework
I've been lucky enough to have my book, "Beginning PHP and MySQL 5, Second Edition", translated into several languages, among them Chinese, Czechoslovakian, Polish and Russian. Further, the English version is available via a variety of outlets in many other countries, France and Germany among them. While my ability to effectively promote the book within these markets is largely negated due to the obvious geographical barriers, the book's support site presents a great opportunity to at least communicate with these readers in their native language.
Of course, despite my best attempts I'm unable to speak this array of languages, but as a Web developer I wanted to create the most efficient means for hosting a multi-lingual support site, and then work with native speakers to translate the English material. Because the support site runs the Zend Framework, naturally I wondered whether some sort of translation component was available, and sure enough a quick perusal of the Zend Framework website turned up Zend_Translate. In this brief tutorial I'll document how I'm using the Zend Framework to make my website available to the world. [....]