Archive for » 08月, 2008 «

星期四, 08月 28th, 2008 | Author: Joshua

Zend Framework 的页面布局模块——Zend_Layout——既可以跟 MVC 一起使用,也可以单独使用。本文只讨论与 MVC 一起使用的情况。

1. 布局脚本

在 application/views 下创建一个 layouts 的文件夹。主布局脚本 layout.phtml 代码如下:

<?php echo $this->doctype(’XHTML1_STRICT’) ?>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<?php echo $this->headTitle() ?>
<?php
$this->headLink()->appendStylesheet(”/styles/main.css”);
// add more links …
?>
<?php echo $this->headLink() ?>
</head>
<body>
<div id=”header”>
<?php echo $this->partial(’header.phtml’) ?>
</div>
<table>
<tr>
<td valign=top>
<div id=”leftcolumn”>
<?php echo $this->partial(’leftcolumn.phtml’) ?>
</div>
</td>
<td valign=top>
<div id=”content”>
<?php echo $this->layout()->content ?>
</div>
</td>
</tr>
</table>
<div id=”footer”>
<?php echo $this->partial(’footer.phtml’) ?>
</div>
</body>
</html>

除了 layout.phtml 之外,还需要编写 header.phtml,leftcolumn.phtml,footer.phtml,以及 main.css 等文件。

Zend Framework 的文档中用一个视图表示了页面布局的应用。

powered by Wordpress Multibox Plugin v1.3.5

2. 设置页面布局

在 MVC 下设置页面布局非常简单,编辑 html/index.php,加入下面两行代码:

/** Setup layout */
require_once ‘Zend/Layout.php’;
Zend_Layout::startMvc($rootPath . ‘/application/views/layouts’);

注意:在启动页面布局后,要调整已有的各个页面,把不需要的 html 元素,如<header> <title> <body> 等去掉。另外,可以通过 $this->headTitle() 来设置页面的题头。

改变页面的布局也很简单,只需在控制器中用下面的代码即可:

$this->_helper->layout->setLayout(’new_layout’);

如果一个控制器所有动作都使用同一个页面布局,可以通过控制器的初始化函数来设置:

public function init() {
parent::init();

$this->_helper->layout->setLayout(’new_layout’);
}

Category: Zend Framework  | Tags:  | Leave a Comment
星期四, 08月 28th, 2008 | Author: Joshua

如果你的项目想要支持多语言版本,那么就需要用到 Zend_Translate。Zend_Translate 的详细文档在这里,不过如果想偷懒的话,也很简单,在 View Helpers 文档中介绍了如何用 Translate Helper 轻松实现多语言支持。

1. 准备翻译文件

Zend_Translate 支持多种格式的翻译文件。选用何种格式可以参考这里。如果条目不是很多(5000条以下),那么可以考虑用最直观的数组格式,而且可以写到一个 php 文件里。假设,我们需要一个中文版支持,翻译文件命名为 zh_cn.php,放在与 application 平行的 languages 文件夹里。该文件内容如下:

<?php
return array(
‘hello_world’ => ‘你好!’,
);

2. 加载翻译文件

编辑 html/index.php 文件,在前端控制器运行之前,插入下面的代码

require_once ‘Zend/Registry.php’;
require_once ‘Zend/Translate.php’;
$adapter = new Zend_Translate(’array’, $rootPath . ‘/languages/zh_cn.php’, ‘zh’);
Zend_Registry::set(’Zend_Translate’, $adapter);

上述代码的作用是载入 zh_cn.php,并把它保存成全局变量。Zend_Registry 可以看成是一个全局变量容器。

注意:在保存到 Zend_Registry 中时,键值必须是 Zend_Translate,否则,得不到应有的结果。

3. 在视图中使用翻译条目

编辑 application/views/scripts/index/index.phtml 文件,将原来的“<h1>Hello World!</h1>”替换成:

<h1><?php echo $this->translate(’hello_world’); ?></h1>

4. 查看页面

这时,浏览器中看到的应是“你好!”。

Category: Zend Framework  | Tags:  | Leave a Comment
星期四, 08月 28th, 2008 | Author: Joshua

使用 Zend Framework 的 MVC 结构,如果不做特殊的处理,所有的异常都会被前端控制器捕获,并累积起来。

Zend Framework 提供了若干种方法来处理异常。这里介绍最简单的一种。

在第一部分《快速上手》中,我们已经有了一个很简单的错误处理机制,即通过 applicatoin/controllers/ErrorController.php 以及其中定义的 errorAction 方法来处理异常。不过,只是给出了一个很简陋的报错信息。现在,我们把它再完善一下,达到两个基本要求:

  1. 用户请求了不存在的控制器或动作(404 错误),在用户的浏览器中给出报错信息;
  2. 其他的系统错误,则在日志文件中记录下来。

1. 创建日志

创建一个与 application 平行的文件夹,命名为 logs。

编辑 html/index.php 文件,加入下面的代码:

require_once ‘Zend/Log.php’;
require_once ‘Zend/Log/Writer/Stream.php’;
$log = new Zend_Log(new Zend_Log_Writer_Stream($rootPath . ‘/logs/errors.log’, ‘a+’));
Zend_Registry::set(’error_log’, $log);

注意:Web 服务器应具有对 logs 文件夹的读写权限。

2. 完善错误处理代码

编辑 application/controllers/ErrorController.php 文件,新的 errrorAction 方法为:

public function errorAction()
{
$log = Zend_Registry::get(’error_log’);
$content = null;
$errors = $this->_getParam (’error_handler’) ;
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER :
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION :
// 404 error — controller or action not found
$this->getResponse ()->setRawHeader ( ‘HTTP/1.1 404 Not Found’ ) ;
// … get some output to display…
$content = “error_page_not_found”$$
break ;
default :
// application error; display error page, but don’t change
// status code
$content = “error_unexpected”$$
// …

// Log the exception
$exception = $errors->exception;

$log->debug($exception->getMessage() .
PHP_EOL . $exception->getTraceAsString());
break ;
}

// Clear previous content
$this->getResponse()->clearBody();
$this->view->content = $content;
}

在 languages/zh_cn.php 中加入错误信息的中文条目(如果你不需要支持多国语言,那么可以忽略这些):

‘error_page_not_found’ => “<h1>404 错误:页面不存在!</h1>” . PHP_EOL .
‘<p>对不起,您所要求的页面不存在。</p>’,
‘error_unexpected’ => ‘<h1>错误!</h1>’ . PHP_EOL .
‘<p>对不起,系统无法响应您的请求。请稍后再试。</p>’,

最后,略微修改一下 application/views/error/error.phtml 文件:

其中的错误显示代码为:

<?php echo $this->translate($this->content); ?>

注意:如果你需要记录所有的异常和错误,包括各种警告,可以参考这里。

Category: Zend Framework  | Tags:  | Leave a Comment