分享

Zend Framework安裝

 sumi2005 2012-05-30

因我版本没找对,那些方法对应的版本太老,我对下面的方法做了一调整,把重复的不合理的,去掉了一部分,我也是刚学

zendframework,说不了太多,不过按照下面的做就对啦

 


Zend Framework安裝
安装环境:
   Windows XP Professional(SP2)
   Appserv(Apache 2.2.4, PHP/5.2.3, MySQL 5.0.45)
   Zend Framework 1.0.1(2007-07-30)
一、基本设置:
1. 设定mod_rewrite
   编辑httpd.conf
   #LoadModule rewrite_module modules/mod_rewrite.so
   如果前面的”#”字在的话,就把它拿掉吧(mod_rewrite的詳細資料,可參考apache网站)
2. 设定include_path
   设定include path之目的,是为了方便在include类別时,省去输入长长一串位置的时间。
a) 可直接修改php.ini之设定
加:
; Windows: "\path1;\path2"
include_path = ".;e:\Program Files\xampp\php\includes\;e:\Program Files\xampp\htdocs\zf\library"
b) 或是于程序中动态加入set_include_path
参考网址:http://tw2./set_include_path
3. 设定httpd.conf之document root
   请参考下一段之目录架构,将document root指向/html,设定完之后,请重新启动Apache,并建议检查一下error.log看是否有错

误的地方。
二、Zend Framework设定
1.基本目录架构
|-/application
   |-/controllers   (MVC之C)
   |-/models    (MVC之M)
   |-/views       (MVC之V)
      |-/filters
      |-/helpers
      |-/scripts
|-/html
   |-.htaccess   (配合url rewrite之资料)
   |-index.php   (bootstrap file)
|-/library
   |-/Zend       (这个是ZF的library,可从ZF网站下载)
2. 檔案设定
a)index.php(bootstrap file),可视个别情况修改
b).htaccess设定:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
   基本上这样设定完,就差不多把环境搭建好了,接下來,就是开始设定各別Controller的工作了。
PS1:在windows系统下要做出.htaccess,可以直接用记事本来做储存的时候,选择「存储类型(T)」为「所有文件」,即可直接输入

档名.htaccess而不会发生错误。
PS2:其它目录也可加个.htaccess档來保护目录里的资料,內容为:deny from all。
三、Controller设定
   先设定一个最基本的IndexController,架构参考上一段落
|-/application
   |-/controllers (MVC之C)
         |-IndexController.php (Index的Controller)     <-新增这个文件
   |-/models    (MVC之M)
   |-/views    (MVC之V)
         |-/filters
         |-/helpers
         |-/scripts
         |-/index          <--新增这个目录
                |-index.phtml     <--新增这个文件
                |-happy.phtml <--新增这个文件
1.   IndexController.php
2.   index.phtml
   这个是indexAction的view,当执行indexAction时,预设会寻找相同的文件名,并render出页面內容
Controller的设定大概这样就完成了(细节可再参考ZF的Document或是其它高手们的Tutorial)
  
   它则是会找IndexController里happy这个action,然后会找happy.phtml来render页面內容。基本上到这里,就把这个小小的MVC

架构做出来了。
自:http://hi.baidu.com/webwlsong/blog/item/33872d2257635ba14623e8a4.html

2. 项目的目录结构
如果你的项目不包含多个模块,可以用下面的目录结构:
|-application/
    |-controllers/
        |-IndexController.php
   |- models/
   |- views/
       |- scripts/
          |-  index/
              |-  index.phtml
       |- helpers/
        |-filters/
|-html/
    |- .htaccess
    |- index.php
如果你的项目要包含多个模块(比如:博客,社区,等等),那么建议使用模块化的目录结构。
3. 网页的根目录

网页的根目录应指向上述目录结构中的 html 文件夹。

4. 重写规则

编辑 html/.htaccess 文件,加入下面两行:

RewriteEngine on

RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

注意:上述是针对 apache 的配置。如果是其他的服务器,请参考这里。

5. 引导程序

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

<?php

require_once 'Zend/Controller/Front.php';

$rootPath = dirname(dirname(__FILE__));

Zend_Controller_Front::run($rootPath . '/application/controllers');上面代码的作用是实例化前端控制器(Front

Controller)并运行它。6. 默认的动作控制器(Action Controller)

Zend Framework 的默认路由规则是 http://域名/控制器名/动作(方法)名。例如:

http:///user/show 会被解析到名为 User 的控制器以及该控制器中定义的 show 方法。如果该方法没有定义,则默认

转到 index 方法。

注意:在代码中,控制器名的后面要加上 Controller,而动作名的后面要加上 Action。

编辑 application/controllers/IndexController.php 文件,输入:

<?php

/** Zend_Controller_Action */

require_once 'Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action

{

    public function indexAction()

    {

    }

}

7. 视图(页面)脚本

编辑 application/views/scripts/index/index.phtml,输入:

<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www./TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

  <meta http-equiv="Content-Type" c />

  <title>My first Zend Framework App</title>

</head>

<body>

    <h1>Hello, World!</h1>

</body>

</html>8. 错误控制器

默认情况下,Zend Framework 的错误处理插件是被注册的。它需要一个错误控制器来处理错误。缺省的错误控制处理被假定为

ErrorController 以及其中定义的 errorAction。

编辑 application/controllers/ErrorController.php,输入:

<?php

/** Zend_Controller_Action */

require_once 'Zend/Controller/Action.php';

class ErrorController extends Zend_Controller_Action

{

    public function errorAction()

    {

    }

}

下面是对应的视图脚本。编辑 application/views/scripts/error/error.phtml,输入:

<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www./TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

  <meta http-equiv="Content-Type" c />

  <title>Error</title>

</head>

<body>

    <h1>An error occurred</h1>

    <p>An error occurred; please try again later.</p>

</body>

</html>9. 运行

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

http://域名
http://域名/index
http://域名/index/index
如果是这样,那么恭喜你!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章