---
title: "Zend Framework 1.10.4手册(ZF的快速启动三)"
author: "yangyz1984"
date: "2010-07-19T16:25:53Z"
tags: ["CSS", "PHP", "XHTML", "Zend", "脚本"]
categories: ["编程语言"]
slug: "zend-framework-1-10-4-zf"
draft: false
lastmod: "2010-07-20T14:42:17Z"
id: 716213
url: "https://yyzstudy.iteye.com/blog/716213"
---

你可能注意到在前面一节提到视图脚本是 HTML 片断——不是完整的网页。这是出于设计的需要；我们希望我们的行为返回的内容只和行为本身有关，而不是整个应用程序。

现在我们必须把得到的内容合成到一个完整的 HTML 网页。我们也想为应用程序创建一个统一的外表和风格。我们将使用一个全站模板来完成这些任务。

Zend Framework 应用模板有两种设计模式：Two Step View 和 Composite View。Two Step View 通常和 Transform View 有关，基本的思想是，你的应用程序的视图创建一个表现，然后为了最后的转换（for final transformation)注入到主模板中。而 Composite View 模式则处理一个由一个或者更多个的原子，应用程序视图组合而成的视图。

在 Zend Framework 中，Zend\_Layout 组合了这些模式的思想。不同于每一个行为视图需要包括全站的东西，他们可以简单的关注它们自己的责任。

偶尔的，然而，你可能需要应用程序方面的信息出现在你的全站视图脚本中，幸运的是，Zend Framework 提供了大量的视图占位符（placeholder）来允许你从你的行为视图脚本提供这样的信息（应用程序方面的信息 application-specific information）。

为了开始使用 Zend\_Layout，首先我们需要通知我们的 bootstrap 来使用 Layout 资源（Bootstrap 类定义了什么资源和组件将要初始化。）。这可以通过使用 zf enable layout 命令来完成：

> % zf enable layout  
> Layouts have been enabled, and a default layout created at application/layouts/scripts/layout.phtml  
> A layout entry has been added to the application config file.

正如命令行提醒的那样，application/configs/application.ini 被更新了，现在 production 部分包括了以下代码：

> ; application/configs/application.ini
> 
> ; Add to \[production] section:  
> resources.layout.layoutPath = APPLICATION\_PATH "/layouts/scripts"

最后的 INI 文件应该看起来是这样的：

> ; application/configs/application.ini
> 
> \[production]  
> ; PHP settings we want to initialize  
> phpSettings.display\_startup\_errors = 0  
> phpSettings.display\_errors = 0  
> includePaths.library = APPLICATION\_PATH "/../library"  
> bootstrap.path = APPLICATION\_PATH "/Bootstrap.php"  
> bootstrap.class = "Bootstrap"  
> appnamespace = "Application"  
> resources.frontController.controllerDirectory = APPLICATION\_PATH "/controllers"  
> resources.frontController.params.displayExceptions = 0  
> resources.layout.layoutPath = APPLICATION\_PATH "/layouts/scripts"
> 
> \[staging : production]
> 
> \[testing : production]  
> phpSettings.display\_startup\_errors = 1  
> phpSettings.display\_errors = 1
> 
> \[development : production]  
> phpSettings.display\_startup\_errors = 1  
> phpSettings.display\_errors = 1

这些指令告诉你的应用程序到 application/layouts/scripts 中查找模板视图脚本。如果你检查你的目录树，你将发现这个目录已经为你创建好了，同时带有 layout.phtml 这个文件。

我们也想确保针对我们的应用程序，我们有一个 XHTML DocType 声明。为了做到这个，我们需要添加一个资源到我们的 bootstrap。

添加一个资源到 bootstrap 的最简单的方法是创建一个以 \_init 短语开头的私有方法。在这个例子中，我们想要初始化 doctype，所以我们在我们的 bootstrap 类中创建一个 \_initDoctype() 方法：

 

[view plain](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)[copy to clipboard](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)[print](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)[?](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)

1. // application/Bootstrap.php
2. **class** Bootstrap **extends** Zend\_Application\_Bootstrap\_Bootstrap
3. {
4. **protected** **function** \_initDoctype()
5. {
6. }
7. }

 

在这个方法内，我们需要提示视图来使用合适的 doctype。但是视图对象从哪里来？简单的方案是，初始化视图（View）资源；一旦我们完成，我们可以从 bootstrap 中扒下视图对象然后使用它。

为了初始化视图资源，把以下代码添加到你的 application/configs/applicationn.ini 文件中，在 production 部分下：

> ; application/configs/application.ini
> 
> ; Add to \[production] section:  
> resources.view\[] =

这将告诉我们初始化视图但是没有选项（\[] 提示 view 键是一个数组，我们什么都没有传递给它）。

现在我们有了一个视图，让我们更新我们的 \_initDoctype() 方法。在它里面，我们首先确保视图资源已经在运行，捕获视图对象，然后配置它：

 

[view plain](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)[copy to clipboard](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)[print](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)[?](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)

01. // application/Bootstrap.php
02. **class** Bootstrap **extends** Zend\_Application\_Bootstrap\_Bootstrap
03. {
04. **protected** **function** \_initDoctype()
05. {
06. $this-&gt;bootstrap('view');
07. $view = $this-&gt;getResource('view');
08. $view-&gt;doctype('XHTML1\_STRICT');
09. }
10. }

 

现在我们已经实例化了 Zend\_Layout 并且设置了 Doctype，让我们创建我们的全站模板：

 

[view plain](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)[copy to clipboard](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)[print](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)[?](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)

01. &lt;!-- application/layouts/scripts/layout.phtml --&gt;
02. &lt;?php echo $this-&gt;doctype() ?&gt;
03. &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
04. &lt;head&gt;
05. &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
06. &lt;title&gt;Zend Framework Quickstart Application&lt;/title&gt;
07. &lt;?php echo $this-&gt;headLink()-&gt;appendStylesheet('/css/global.css') ?&gt;
08. &lt;/head&gt;
09. &lt;body&gt;
10. &lt;div id="header" style="background-color: #EEEEEE; height: 30px;"&gt;
11. &lt;div id="header-logo" style="float: left"&gt;
12. &lt;b&gt;ZF Quickstart Application&lt;/b&gt;
13. &lt;/div&gt;
14. &lt;div id="header-navigation" style="float: right"&gt;
15. &lt;a href="&lt;?php echo $this-&gt;url(
16. **array**('controller'=&gt;'guestbook'),
17. 'default',
18. true) ?&gt;"&gt;Guestbook&lt;/a&gt;
19. &lt;/div&gt;
20. &lt;/div&gt;
21. &lt;?php echo $this-&gt;layout()-&gt;content ?&gt;
22. &lt;/body&gt;
23. &lt;/html&gt;

 

我们使用 layout() 视图帮助器捕获应用程序的内容，同时访问 content 键。如果你愿意的话，你可以呈现其它的片断，但在大多数情况下，这是所需的全部了。

同时注意 headLink() 占位符的使用。这是为生成 HTML   &lt;link&gt; 元素的简单方式，同时还可以在你的应用程序内跟踪它们。如果你需要增加额外的 CSS 样式表来支持一个单独的行为，你可以这样做，并确保它会在最后的呈现页面中出现。

注意，检查  
现在打开 http://localhost 并检查源代码，你应该看见你的 XHTML 头部，头部，标题和正文部分。

 

![](http://dl.iteye.com/upload/attachment/279991/2f35589a-7a89-331a-bd5f-0ab5463ee7da.jpg)

 

[view plain](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)[copy to clipboard](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)[print](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)[?](http://blog.csdn.net/klinghr/archive/2010/03/23/5406515.aspx)

01. &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"**&gt;&lt;html** xmlns="http://www.w3.org/1999.xhtml"**&gt;**
02. **&lt;head&gt;**
03. **&lt;meta** http-equiv="Content-Type" content="text/html; charset=utf-8" **/&gt;**
04. **&lt;title&gt;**Zend Framework Quickstart Application**&lt;/title&gt;**
05. **&lt;link** href="/css/global.css" media="screen" rel="stylesheet" type="text/css" **/&gt;&lt;/head&gt;**
06. **&lt;body&gt;**
07. **&lt;div** id="header" style="background-color: #EEEEEE; height: 30px;"**&gt;**
08. **&lt;div** id="header-logo" style="float: left"**&gt;**
09. **&lt;b&gt;**ZF Quickstart Application**&lt;/b&gt;**
10. **&lt;/div&gt;**
11. **&lt;div** id="header-navigation" style="float: right"**&gt;**
12. **&lt;a** href="/guestbook"**&gt;**Guestbook**&lt;/a&gt;**
13. **&lt;/div&gt;**
14. **&lt;/div&gt;**
15. **&lt;style&gt;**
16. a:link,
17. a:visited
18. {
19. color: #0398CA;
20. }
21. span#zf-name
22. {
23. color: #91BE3F;
24. }
25. div#welcome
26. {
27. color: #FFFFFF;
28. background-image: url(http://framework.zend.com/images/bkg\_header.jpg);
29. width:  600px;
30. height: 400px;
31. border: 2px solid #444444;
32. overflow: hidden;
33. }
34. div#more-information
35. {
36. background-image: url(http://framework.zend.com/images/bkg\_body-bottom.gif);
37. height: 100%;
38. }
39. **&lt;/style&gt;**
40. **&lt;center&gt;**
41. **&lt;div** id="welcome"**&gt;**
42. **&lt;br** **/&gt;**
43. **&lt;h1&gt;**Welcome to the **&lt;span** id="zf-name"**&gt;**Zend Framework!**&lt;/span&gt;&lt;/h1&gt;**
44. **&lt;h3&gt;**This is your project's main page**&lt;/h3&gt;&lt;br** **/&gt;&lt;br** **/&gt;**
45. **&lt;h4&gt;&lt;a** href="zf.html"**&gt;**click here**&lt;/a&gt;&lt;/h4&gt;**
46. **&lt;div** id="more-information"**&gt;**
47. **&lt;br** **/&gt;**
48. **&lt;img** src="http://framework.zend.com/images/PoweredBy\_ZF\_4LightBG.png" **/&gt;&lt;br** **/&gt;&lt;br** **/&gt;**
49. Helpful Links: **&lt;br** **/&gt;**
50. **&lt;a** href="http://framework.zend.com/"**&gt;**Zend Framework Website**&lt;/a&gt;** |
51. **&lt;a** href="http://framework.zend.com/manual/en/"**&gt;**Zend Framework Manual**&lt;/a&gt;**
52. **&lt;/div&gt;**
53. **&lt;/div&gt;**
54. **&lt;/center&gt;**
55. **&lt;/body&gt;**
56. **&lt;/html&gt;**