Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6541871
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Python/Ruby

2011-08-12 11:11:49

下载最新的ZendFrameWork1.11.10最小版本,下载地址可以从官方下载,或者从下面的地址:

http://blog.chinaunix.net/space.php?uid=78707&do=blog&id=2169031

 

环境Apache2.2.xPhp5.2.x

Apache开启mod_rewrite功能

必须保证Apache已配置成支持.htaccess文件的模式。通常这可以通过在httpd.conf中将

AllowOverride None

改成

AllowOverride All

 

目录结构

虽然Zend Framework对目录结构没有特别要求,但其手册上还是推荐了一种常用的目录结构,本教程也使用这种目录结构。这种结构要求你能完全控制Apache的配置文件,以便可以将大多数的文件存放在web的根目录之外。

首先在web服务器的根目录下创建一个ZendApp目录,然后分别创建下面的子目录来存放网站的文件:

引导文件

Zend Framework控制器类 Zend_Controller支持网站使用干净的URL5。为此所有的请求都需要通过index.php进入。这就是通常所说的前端控制器(Front Controller)设计模式。它为我们的应用程序的所有页面提供了一个中心控制点并确保程序的运行环境已经正确设置。要完成这一切,都必须在ZendApp目录下创建一个.htaccess文件:

 

  1. # Zend Framework rewrite规则
  2. RewriteEngine on
  3. RewriteCond %{REQUEST_FILENAME} !-f
  4. RewriteRule .* index.php
  5. # 安全性考虑:不允许列表目录内容
  6. Options -Indexes

引导文件: index.php

  1. <?php
  2. error_reporting(E_ALL|E_STRICT);
  3. ini_set('display_errors', 1);
  4. date_default_timezone_set('Europe/London');
  5. // 目录设置和类装载

  6. set_include_path('.' . PATH_SEPARATOR . './library/'
  7. . PATH_SEPARATOR . './application/models'
  8. . PATH_SEPARATOR . get_include_path());
  9. // include "Zend/Loader.php";

  10. // Zend_Loader::registerAutoload();

  11. require_once 'Zend/Loader/Autoloader.php';
  12. Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
  13. // 设置控制器

  14. $frontController = Zend_Controller_Front::getInstance();
  15. // 如果设置虚拟主机,则下面两行不用设置;如果没有设置虚拟主机,则需要采用下面的两行中的一种方式

  16. $frontController->setParam('useDefaultControllerAlways', true);
  17. //$frontController->setBaseUrl('/ZendApp');

  18. $frontController->throwExceptions(true);
  19. $frontController->setControllerDirectory('./application/controllers');
  20. Zend_Layout::startMvc(array('layoutPath'=>'./application/layouts'));
  21. // run!

  22. $frontController->dispatch();

需要的页面:

indexaddeditdelete

编写控制器

ZendApp/application/controllers/IndexController.php

  1. <?php
  2. class IndexController extends Zend_Controller_Action
  3. {
  4.     function indexAction()
  5.     {
  6.         $this->view->title="My Albums";
  7.     }
  8.     function addAction()
  9.     {
  10.         $this->view->title="Add New Albums";
  11.     }
  12.     function editAction()
  13.     {
  14.         $this->view->title="Edit Albums";
  15.     }
  16.     function deleteAction()
  17.     {
  18.         $this->view->title="Delete Albums";
  19.     }
  20. }

编写视图

下面的内容,分别存四个文件:

ZendApp\application\views\scripts\index\add.phtml

ZendApp\application\views\scripts\index\index.phtml

ZendApp\application\views\scripts\index\edit.phtml

ZendApp\application\views\scripts\index\delete.phtml
  1. <html>
  2. <head>
  3. <title><?php echo $this->escape($this->title);?></title>
  4. </head>
  5. <body>
  6. <h1><?php echo $this->escape($this->title);?></h1>
  7. </body>
  8. </html>

ZendApp\application\layouts\layout.phtml内容如下:

 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "">
  3. <html xmlns="" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  6. <title><?php echo $this->escape($this->title); ?></title>
  7. </head>
  8. <body>
  9. <div id="content">
  10. <h1><?php echo $this->escape($this->title);?></h1>
  11. <?php echo $this->layout()->content; ?>
  12. <div>
  13. </body>
  14. </html>

通过上面的搭建,基本上就能看出Zend FrameworkMVC的简单应用了。

 

注意:

1       Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (zendapp)' in

修改办法:

1、  修改httpd.conf,建立虚拟主机。(官方建议)

    ServerName 127.0.0.1

    DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/ZendApp/public"

    SetEnv APPLICATION_ENV "development"

   

        DirectoryIndex index.php

        AllowOverride All

        Order allow,deny

        Allow from all

   

2、  index.php中加入:

$frontController->setParam('useDefaultControllerAlways', true);

3、  还有人说加入:

$frontController->setBaseUrl('homepath’')//homepath替换你自己的public所在的路径

1       Notice: Zend_Loader::Zend_Loader::registerAutoload is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader instead in

发生上述提示,修改下面两行为

 include "Zend/Loader.php";

 Zend_Loader::registerAutoload();

到:

require_once 'Zend/Loader/Autoloader.php';

Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);

 

阅读(3333) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~