Zend Framework 1.10.4手册(ZF的快速启动二)



 

 

 为了创建您的项目,你必须先下载并解压缩Zend Framework。

最简单的方法是安装 Zend Server 。Zend Server已经有 Mac OSX,Windows,Fedora Core,和 Ubuntu 版本,以及一个与大多数 Linux 发行版本相兼容通用的安装包。

安装 Zend Server 以后,Zend Framework 文件可能会在 Mac OSX 和 Linux 下的 /usr/local/zend/share/ZendFramework,在 Windows 下 C:\Program Files\Zend\ZendServer\share\ZendFramework 找到。 Zend Framework 已经被包括include_path

如果不打算安装 Zend Server,您可以 »下载Zend Framework的最新版本 然后解压缩。记住文件解压缩后所在位置。

解压缩以后,可以把 Zend Framework 文件夹下的 library/ 子目录添加到 php.ini 的 include_path 设置中。比如解压缩以后,Zend Framework 放置在 C:\program files\下,打开 php.ini,找到 include_path 这一行,如果前面有注释符,把注释符去掉,然后整句修改为:

include_path = ".;C:\program files\Zend Framework\library"

就是这样! 框架现在已经安装并可以使用。

创建项目

zf 命令行工具

在您的Zend Framework文件夹中,有一个 bin/ 子目录,包含 zf.sh 脚本(适合 Unix 用户)和 zf.bat 脚本(基于 Windows 用户)。

记下这个脚本绝对路径。

下文只要你看到的命令 zf ,请以绝对路径替换它。 比如你的 zf.bat 文件在 C:/program files/zend framework/bin/zf.bat,那么下面只要是 zf 的命令,就用 C:/program files/zend framework/bin/zf.bat 替换。

在 Unix 系统,您可能需要把 zf 添加到 shell: alias zf.sh=path/to/ZendFramework/bin/zf.sh .

如果您对建立zf 命令行工具有疑问,请参考 手册

打开一个终端(在 Windows 中, Start -> Run   ,然后使用 cmd)。

导航到你想建立项目的文件夹。

执行下列操作:

  1.  % zf create project quickstart

运行此命令将创建基本的网站结构,包括您的初始化的控制器和视图。

该树如下所示:

  1.  quickstart
  2.  |-- application
  3.  |   |-- Bootstrap.php
  4.  |   |-- configs
  5.  |   |   `-- application.ini
  6.  |   |-- controllers
  7.  |   |   |-- ErrorController.php
  8.  |   |   `-- IndexController.php
  9.  |   |-- models
  10.  |   `-- views
  11.  |       |-- helpers
  12.  |       `-- scripts
  13.  |           |-- error
  14.  |           |   `-- error.phtml
  15.  |           `-- index
  16.  |               `-- index.phtml
  17.  |-- library
  18.  |-- public
  19.  |   |-- .htaccess
  20.  |   `-- index.php
  21.  |-- tests
  22.      |-- application
  23.      |   `-- bootstrap.php
  24.      |-- library
  25.      |   `-- bootstrap.php
  26.      | `-- phpunit.xml

此时,如果您没有将 Zend Framework 包括到 include_path, 我们建议,把下载以后解压缩的 zend framework 目录下的 library/ 里面的 Zend/ 整个文件夹,复制到你刚才建立的项目下的 library/ 目录中。或者建立一个软链接。

在这2种情况下,你需要递归复制或者在 library/Zend 目录和你的Zend Framework 项目的文件目录之间创建软连接。

在Unix类系统,这将类似于下列之一:

  1.  # Symlink:
  2.  % cd library; ln -s path/to/ZendFramework/library/Zend . 
  3.  # Copy
  4.  % cd library; cp -r path/to/ZendFramework/library/Zend .

在Windows系统上,通过文件浏览器很容易办到这些。

现在,项目已经创建,下面主要目标将是理解:bootstrap, configuration(设置), action controllers(行为控制器), 和 views(视图)。

 The Bootstrap

Bootstrap 类定义了什么资源和组件将要初始化。

默认情况下,Zend Framework的前端控制器 被初始化,它使用 application/controllers/ 作为默认目录,在其中寻找行动控制器(更多稍后)。

类如下所示:

 

  1. // application/Bootstrap.php   
  2.     
  3. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap   
  4. {   
  5. }   

// application/Bootstrap.php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { }

 

正如你所看到的,开始并不需要做多少事情。

 配置

虽然 Zend Framework 本身不需要配置,你经常需要配置您的应用程序。  

默认配置是放置在 application/configs/application.ini 文件中,这个文件了包含一些基本的指令设置你的 PHP 环境(例如,打开还是关闭错误报告),指示到 bootstrap 类的路径(以及它的类的名称),以及到行为控制器的路径。

它看起来如下:

  1.  ; application/configs/application.ini
  2.  [ production ]
  3.  phpSettings. display_startup_errors = 0
  4.  phpSettings. display_errors = 0
  5.  includePaths. library = APPLICATION_PATH "/../library"
  6.  bootstrap. path = APPLICATION_PATH "
  7.  bootstrap. class = "Bootstrap"
  8.  appnamespace = "Application"
  9.  resources.frontController. controllerDirectory = APPLICATION_PATH "/controllers"
  10.  resources.frontController.params. displayExceptions = 0
  11.  [ staging : production ]
  12.  [ testing : production ]
  13.  phpSettings. display_startup_errors = 1
  14.  phpSettings. display_errors = 1
  15.  [ development : production ]
  16.  phpSettings. display_startup_errors = 1
  17.  phpSettings. display_errors = 1

有关此文件有几点应该指出的。

首先,在使用的 INI 格式的配置,你可以直接引用常量和扩展他们; APPLICATION_PATH 实际上是一个常量。

另外注意到,被定义的这几个部分:production, staging, testing, and development。

后三个继承自“production”环境。

这是组织配置一个有效的方法,从而确保在应用程序开发的每一阶段都能有合适的配置。

 Action Controllers

您的应用程序的行为控制器 包含您的应用程序的工作流程,并把您的请求映射到适当的模型和视图。

一个行为控制器应该有一个或多个方法以“Action”结尾;然后可以通过网络请求这些方法。  

默认情况下,Zend Framework 的 URL 遵循 /constroller/action 结构,其中“controller”映射行为控制器的名称(除去“Controller”后缀)同时“action”映射到 action 的方法(除去 “Action”后缀)。

通常,你总是需要一个 IndexController,这是一个 fallback 控制器,它也服务该网站的主页,同时需要一个 ErrorController,这是用来处理,如 HTTP 404 错误(未找到控制器或行动)和 HTTP 500 错误(应用程序错误)这些事情。

默认 IndexController 如下:

 

  1.  // application/controllers/IndexController.php     
  2.  class IndexController extends Zend_Controller_Action    
  3.  {     
  4.  public function init ( )     
  5.  {     
  6.  /* Initialize action controller here   
  7.  }     
  8.  public function indexAction ( )     
  9.  {     
  10.  // action body <br />   
  11.  }     
  12.  }    

 <wbr>// application/controllers/IndexController.php <wbr>  <wbr>class IndexController extends Zend_Controller_Action  <wbr>{ <wbr>  <wbr>public function init ( ) <wbr>  <wbr>{ <wbr>  <wbr>/* Initialize action controller here  <wbr>} <wbr>  <wbr>public function indexAction ( ) <wbr>  <wbr>{ <wbr>  <wbr>// action body <br />  <wbr>} <wbr>  <wbr>} <wbr>

 

默认ErrorController如下:

 

  1.  // application/controllers/ErrorController.php     
  2.  class ErrorController extends Zend_Controller_Action    
  3.  {     
  4.  public function errorAction ( )     
  5.  {     
  6.  $errors = $this ->_getParam ( 'error_handler' ) ;     
  7.  switch ( $errors -> type ) {     
  8.  case Zend_Controller_Plugin_ErrorHandler:: EXCEPTION_NO_ROUTE :   
  9.  case Zend_Controller_Plugin_ErrorHandler:: EXCEPTION_NO_CONTROLLER :    
  10.  case Zend_Controller_Plugin_ErrorHandler:: EXCEPTION_NO_ACTION :    
  11.  // 404 error -- controller or action not found   
  12.  $this -> getResponse ( ) -> setHttpResponseCode ( 404 ) ;    
  13.  $this -> view -> message = 'Page not found' ;    
  14.  break ;     
  15.  default :     
  16.  // application error      
  17.  $this -> getResponse ( ) -> setHttpResponseCode ( 500 ) ;   
  18.  $this -> view -> message = 'Application error' ;   
  19.  break ;   
  20.  }     
  21.  $this -> view -> exception = $errors -> exception ;   
  22.  $this -> view -> request = $errors -> request ;    
  23.  }      
  24.  }     

 <wbr>// application/controllers/ErrorController.php <wbr>  <wbr>class ErrorController extends Zend_Controller_Action  <wbr>{ <wbr>  <wbr>public function errorAction ( ) <wbr>  <wbr>{ <wbr>  <wbr>$errors = $this ->_getParam ( 'error_handler' ) ; <wbr>  <wbr>switch ( $errors -> type ) { <wbr>  <wbr>case Zend_Controller_Plugin_ErrorHandler:: EXCEPTION_NO_ROUTE :  <wbr>case Zend_Controller_Plugin_ErrorHandler:: EXCEPTION_NO_CONTROLLER :  <wbr>case Zend_Controller_Plugin_ErrorHandler:: EXCEPTION_NO_ACTION :  <wbr>// 404 error -- controller or action not found  <wbr>$this -> getResponse ( ) -> setHttpResponseCode ( 404 ) ;  <wbr>$this -> view -> message = 'Page not found' ;  <wbr>break ; <wbr>  <wbr>default : <wbr>  <wbr>// application error  <wbr>  <wbr>$this -> getResponse ( ) -> setHttpResponseCode ( 500 ) ;  <wbr>$this -> view -> message = 'Application error' ;  <wbr>break ;  <wbr>} <wbr>  <wbr>$this -> view -> exception = $errors -> exception ;  <wbr>$this -> view -> request = $errors -> request ;  <wbr>} <wbr>  <wbr>} <wbr>

 

您会注意到,(1)IndexController没有包含真正的代码,并且(2)ErrorController 对一个 "view"的属性做了一个引用。这就使我们很好地进入下一个议题。

 视图

在 Zend Framework 中视图是用老式的PHP写成。  

视图脚本放置在 application/views/scripts/,他们在那里继续使用控制器名称进行分类。

就我们这个例子而言,我们有一个 IndexController 和 ErrorController,所以,我们在我们的视图脚本目录,相应的就有 index/ 和 error/ 子目录。  

在这些子目录,你可以查找和创建与每一个行为控制器对应的视图脚本,在默认情况下,我们于是有了视图脚本  index/index.phtml 和 error/error.phtml。

视图脚本可以包含任何您想要标记,并使用 <?php 开始标记和 ?> 结束标记来插入 PHP 指令。

以下是我们默认安装的 index/index.phtml 视图脚本:

 

 

error/error.phtml 视图脚本因为它使用了一些 PHP 条件语句显得更有趣:

 

创建一个虚拟主机

为了这个快速入门,我们假设你正在使用 Apache web 服务器。Zend Framework 在其它的服务器上工作良好--包括微软的 IIS,lighttpd,nginx,以及更多--但是更多的开发者至少都熟悉 Apache,它提供一个简单的引入 Zend Framework 的目录结构和重写功能。

为了创建你的虚拟主机,你应该知道你的 httpd.conf 文件所在的位置,以及潜在的其它配置文件所在的位置。一些通常的位置是:

      /etc/httpd/httpd.conf (Fedora, RHEL, and others)

      /etc/apache2/httpd.conf (Debian, Ubuntu, and others)

      /usr/local/zend/etc/httpd.conf (Zend Server on *nix machines)

      C:\Program Files\Zend\Apache2\conf (Zend Server on Windows machines)

在你的 httpd.conf(或者某些系统上的 httpd-vhosts.conf 文件),你将需要做两件事情。首先,确保 NameVirtualHost 已经被定义;通常的,你会把它设置成 *:80 这个值。其次,定义一个虚拟主机:

<VirtualHost *:80>
          ServerName quickstart.local
          DocumentRoot /到 quickstart 的路径/quickstart/public

          SetEnv APPLICATION_ENV "development"

          <Directory /到 quickstart 的路径/quickstart/public>
              DirectoryIndex index.php
              AllowOverride All
              Order allow,deny
              Allow from all
          </Directory>
</VirtualHost>

这里需要注意几件事。首先,注意 DocumentRoot 设置指向了我们项目的 public 子目录;这意味着只有在那个目录下的文件能被服务器所服务。其次,注意 AllowOverride,Order,和 Allow 指令;它们允许我们在我们的项目内使用 htaccess 文件,这是一个好的实践,因为它能阻止当你对你的站点改变指令的时候,频繁的重启 web 服务器;然而,在实战中,你应该把你的 htaccess 文件的内容放到你的服务器配置里面并禁止这样做。第三,注意 SetEnv 指令。我们在这里所做的是为了你的虚拟主机设置一个环境变量;这个变量将被 index.php 拾起,并用来为我们的 Zend Framework 应用程序设置 APPLICATION_ENV 常量。在实战中,你可以取消这个指令(在那种情况下,它将默认 production)或者把它精确的设置为 production。

最后,你将需要添加一条记录到你的 hosts 文件中,以反映你在 ServerName 指令中设置的值。在 *nix 系统中,这通常是 /etc/hosts;在 Windows,你通常会在 C:\WINDOWS\system32\drivers\etc 中找到它。不管是什么系统,这条记录看起来是这样的:

127.0.0.1 quickstart.local

启动你的 web 服务器(或者重启),你应该准备就绪了。

检查

在此时,你应该可以启动 Zend Framework 应用程序了。把你的浏览器指向你在之前设置好的服务器的名字(server name);你应该能看到一个欢迎页面。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值