这个系列是我的学习笔记。主要内容来自 Zend Framework 的[程序员参考手册],结合了自己的学习和开发过程。

1. 安装

从 Zend Framework 的网页上下载最新版本。解压后,把整个目录拷贝到一个理想的地方,比如:/php/library/Zend。

打开 php.ini 文件,确认包含 Zend 目录的路径在 include_path 里定义了。以上面的配置为例,php.ini 中应有类似下面的条目:

include_path = “.:/php/library”

注意:Windows 下的写法略有不同,应该类似于 include_path = “.;C:\php\library”

初始的安装就这么简单。Zend Framework 的一些组件会用到 php 的一些附加模块。具体的要求请参考这里

 

2. 项目的目录结构

如果你的项目不包含多个模块,可以用下面的目录结构:

<span style="color: rgb(0, 0, 0);">application/<br />    controllers/<br />        IndexController.php<br />    models/<br />    views/<br />        scripts/<br />            index/<br />                index.phtml<br />        helpers/<br />        filters/<br />html/<br />    .htaccess<br />    index.php<br /></span></pre>
<p>如果你的项目要包含多个模块(比如:博客,社区,等等),那么建议使用<a href="http://framework.zend.com/manual/en/zend.controller.modular.html">模块化的目录结构</a>。</p>
<p> </p>
<p><strong>3. 网页的根目录</strong></p>
<p>网页的根目录应指向上述目录结构中的 html 文件夹。</p>
<p> </p>
<p><strong>4. 重写规则</strong></p>
<p>编辑 html/.htaccess 文件,加入下面两行:</p>
<pre class="programlisting"><span style="color: rgb(0, 0, 0);">RewriteEngine on<br />RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php<br /></span></pre>
<p><em><strong>注意:</strong></em>上述是针对 apache 的配置。如果是其他的服务器,请参考<a href="http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.introduction">这里</a>。</p>
<p> </p>
<p><strong>5. 引导程序</strong></p>
<p>编辑 html/index.php 文件,敲入下面代码:</p>
<pre><span style="color: rgb(0, 0, 0);"><br /><span style="color: rgb(29, 83, 193);"><?php<br /></span><span style="color: rgb(35, 45, 48);">require_once </span><span style="color: rgb(127, 30, 101);">'Zend/Controller/Front.php'</span><span style="color: rgb(35, 45, 48);">;<br />$rootPath = dirname(dirname(__FILE__));<br /></span><span style="color: rgb(29, 83, 193);">Zend_Controller_Front</span><span style="color: rgb(35, 45, 48);">::</span><span style="color: rgb(29, 83, 193);">run</span><span style="color: rgb(35, 45, 48);">(</span></span><span style="color: rgb(0, 0, 0);"><span style="color: rgb(35, 45, 48);">$rootPath . </span></span><span style="color: rgb(0, 0, 0);"><span style="color: rgb(127, 30, 101);">'/application/controllers'</span><span style="color: rgb(35, 45, 48);">);</span><br /></span><br /></pre>
<p>上面代码的作用是实例化前端控制器(Front Controller)并运行它。</p>
<p> </p>
<p><strong>6. 默认的动作控制器(Action Controller)</strong></p>
<p>Zend Framework 的默认路由规则是 http://域名/控制器名/动作(方法)名。例如:</p>
<p>http://example.com/user/show 会被解析到名为 User 的控制器以及该控制器中定义的 show 方法。如果该方法没有定义,则默认转到 index 方法。</p>
<p><em><strong>注意:</strong></em>在代码中,控制器名的后面要加上 Controller,而动作名的后面要加上 Action。</p>
<p>编辑 application/controllers/IndexController.php 文件,输入:</p>
<pre class="programlisting"><span style="color: rgb(0, 0, 0);"><br /><span style="color: rgb(29, 83, 193);"><?php<br /></span><span style="color: rgb(123, 141, 154);">/** Zend_Controller_Action */<br /></span><span style="color: rgb(35, 45, 48);">require_once </span><span style="color: rgb(127, 30, 101);">'Zend/Controller/Action.php'</span><span style="color: rgb(35, 45, 48);">;<br /><br />class </span><span style="color: rgb(29, 83, 193);">IndexController </span><span style="color: rgb(35, 45, 48);">extends </span><span style="color: rgb(29, 83, 193);">Zend_Controller_Action<br /></span><span style="color: rgb(35, 45, 48);">{<br />    public function </span><span style="color: rgb(29, 83, 193);">indexAction</span><span style="color: rgb(35, 45, 48);">()<br />    {<br />    }<br />}</span><br /></span><br /></pre>
<p><strong>7. 视图(页面)脚本</strong></p>
<p>编辑 application/views/scripts/index/index.phtml,输入:</p>
<pre class="programlisting"><span style="color: rgb(0, 0, 0);"><!DOCTYPE html<br />PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"<br />"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><br /><html><br /><head><br />  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br />  <title>My first Zend Framework App</title><br /></head><br /><body><br />    <h1>Hello, World!</h1><br /></body><br /></html></span></pre>
<p> </p>
<p><strong>8. 错误控制器</strong></p>
<p>默认情况下,Zend Framework 的错误处理插件是被注册的。它需要一个错误控制器来处理错误。缺省的错误控制处理被假定为 ErrorController 以及其中定义的 errorAction。</p>
<p>编辑 application/controllers/ErrorController.php,输入:</p>
<pre class="programlisting"><span style="color: rgb(0, 0, 0);"><br /><span style="color: rgb(29, 83, 193);"><?php<br /></span><span style="color: rgb(123, 141, 154);">/** Zend_Controller_Action */<br /></span><span style="color: rgb(35, 45, 48);">require_once </span><span style="color: rgb(127, 30, 101);">'Zend/Controller/Action.php'</span><span style="color: rgb(35, 45, 48);">;<br /><br />class </span><span style="color: rgb(29, 83, 193);">ErrorController </span><span style="color: rgb(35, 45, 48);">extends </span><span style="color: rgb(29, 83, 193);">Zend_Controller_Action<br /></span><span style="color: rgb(35, 45, 48);">{<br />    public function </span><span style="color: rgb(29, 83, 193);">errorAction</span><span style="color: rgb(35, 45, 48);">()<br />    {<br />    }<br />}</span><br /></span><br /></pre>
<p>下面是对应的视图脚本。编辑 application/views/scripts/error/error.phtml,输入:</p>
<pre class="programlisting"><span style="color: rgb(0, 0, 0);"><!DOCTYPE html<br />PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"<br />"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><br /><html><br /><head><br />  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br />  <title>Error</title><br /></head><br /><body><br />    <h1>An error occurred</h1><br />    <p>An error occurred; please try again later.</p><br /></body><br /></html></span>

 

9. 运行

好,现在运行网站。在浏览器中键入下面三个地址,得到的结果应该是一样的——就是最最常见的“Hello, World!“。

  • http://域名
  • http://域名/index
  • http://域名/index/index

如果是这样,那么恭喜你!

A content management system(CMS) is a computer application used to create, edit, manage, and publish content in a consisitently organized fashion. CMSs are frequently used for storing,controlling,versioning, and publishing industry-specific documentation such as news articles,operators’ manuals, technical manuals,sales guides, and marketing brochures(小册子).The content managed may include computer files,image media, audio files, video files, electronic documents, and Web content.
A CMS may support the following features:
identification of all key users and their content management roles;
the ability to assign roles and responsibilities to different content categories or types;
definition of workflow tasks for collaborative creation, often coupled with event messaging so that content managers are alerted to changes in content(For example, a content creator submits a story, which is published only after the copy editor revises it and the editor-in-chief approves it.);
the ability to track and manage multiple versions of a single instance of content;
the ability to capture content(e.g. scanning);
the ability to publish the content to a repository to support access to the content(Increasingly(越来越多地), the repository is an inherent part of the sytem, and incorporates(包含,加上) enterprise search and retrieval.);