Zend Framework1.10.1windows环境下安装配置.

Zendframework安装,学习来源于思考,多看多想多做,总会有进步的,有成果的那一刻是多么激动啊.

 

Zend Framework安裝
安装环境:
   Windows XP Professional(SP
3)
   Apache 2.2.15, PHP/5.2.
29, MySQL 5.1.33
   Zend Framework 1.
10.1(2009-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"

如果无权控制php.ini文件,也不必担心;可以把以下指令放在.htaccess文件中,这个文件应当放在服务器的文档根目录中(document_root):

php_value  include_path “e:/Program Files/xampp/php/includes/;e:/Program Files/xampp/htdocs/zf/library
b)
或是于程序中动态加入set_include_path
参考网址:http://tw2.php.net/set_include_path

3. 设定httpd.confdocument root

虚拟机的设定:

<VirtualHost *:80>

  ServerName mydomain.com

  DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/mydomain.com/document_root"

 

  <Directory "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/mydomain.com/document_root">

    AllowOverride All

    Options All  

  </Directory>

</VirtualHost>
  
请参考下一段之目录架构,将document_root指向/document_root,设定完之后,请重新启动Apache,并建议检查一下error.log看是否有错误的地方。

二、Zend Framework设定
1.
基本目录架构
|-/application
   |-/controllers   (MVC
C)

|-IndexController.php
   |-/models    (MVC
M)
   |-/views       (MVC
V)
      |-/filters
      |-/helpers
      |-/scripts

|-/index.phtml
|-/
document_root
   |-.htaccess   (
配合url rewrite之资料)
   |-index.php   (bootstrap file)
|-/library
   |-/Zend       (
这个是ZFlibrary,可从ZF网站下载)

2. 檔案设定
a)index.php(bootstrap file)
,可视个别情况修改

<?php

define('ZFW_VERSION','1.10.1');

define('ZFW_PREFIX','C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/mydomain.com/library');

define('APP_PATH',realpath(dirname(_FILE_) . '/../'));

 

$paths=array(

  APP_PATH,

  APP_PATH.DIRECTORY_SEPARATOR.'application'.DIRECTORY_SEPARATOR.'models',

  ZFW_PREFIX.DIRECTORY_SEPARATOR.'ZendFramework-'.ZFW_VERSION.DIRECTORY_SEPARATOR.'library',

  get_include_path()

);

 

set_include_path(implode(PATH_SEPARATOR,$paths));

require_once('Zend/Loader.php');

 

Zend_Loader::registerAutoload();

$front=Zend_Controller_Front::getInstance();

$front->throwExceptions(true);

$front->run(APP_PATH.'/application/controllers');

?>
b).htaccess
设定:
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s [OR]

RewriteCond %{REQUEST_FILENAME} -l [OR]

RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ - [NC,L]

RewriteRule ^.*$ /index.php [NC,L]
  
基本上这样设定完,就差不多把环境搭建好了,接下來,就是开始设定各別Controller的工作了。
PS1:
windows系统下要做出.htaccess,可以直接用记事本来做储存的时候,选择「存储类型(T)」为「所有文件」,即可直接输入

档名.htaccess而不会发生错误。
PS2:
其它目录也可加个.htaccess档來保护目录里的资料,內容为:deny from all

 

1.   IndexController.php
2.   index.phtml
  
这个是indexActionview,当执行indexAction时,预设会寻找相同的文件名,并render出页面內容
Controller
的设定大概这样就完成了(细节可再参考ZFDocument或是其它高手们的Tutorial)
  
  
它则是会找IndexControllerindex这个action,然后会找index.phtmlrender页面內容。基本上到这里,就把这个小小的MVC

三、Controller设定

项目的目录结构
如果你的项目不包含多个模块,可以用下面的目录结构:
|-application/
    |-controllers/
        |-IndexController.php
   |- models/
   |- views/
       |- scripts/
          |-  index/
              |-  index.phtml
       |- helpers/
        |-filters/
|-
document_root/
    |- .htaccess
    |- index.php

|-libraty

|-Zend
如果你的项目要包含多个模块(比如:博客,社区,等等),那么建议使用模块化的目录结构。

1.        IndexController.php

<?php

 

/**

* IndexController - The default controller class

*

* @author

* @version

*/

 

require_once 'Zend/Controller/Action.php';

 

//默认控制器类

class IndexController extends Zend_Controller_Action {

/**

* The default action - show the home page

*/

    public function indexAction() {

       //动作方法名:对应的文件是views/scripts/index/index.phtml

       $this->view->name='Kevin';     

    }

       

public function showAction() {

      //新增的动作方法名:对应的文件是views/scripts/index/show.phtml

   // TODO Auto-generated LoveController::indexAction() default action

}

 

}

 

?>
2.   index.phtml

<!DOCTYPE html

 

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

 

"http://www.w3.org/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>

    <p>Welcome:<?php echo $this->name;?></p>

 

</body>

 

</html>

你可能仔细的,已经发现了一个启用视图的操作:(在IndexController.php中)

Public function indexAction(){

  $this->view->name=’Kevin’;

}
  
这个是indexActionview,当执行indexAction时,预设会寻找相同的文件名,并render出页面內容
Controller
的设定大概这样就完成了(细节可再参考ZFDocument或是其它高手们的Tutorial)
  
  
它则是会找IndexControllerindex这个action,然后会找index.phtmlrender页面內容。基本上到这里,就把这个小小的MVC架构做出来了。

 

下面让我们总结一下整个MVC:

1. 网页的根目录

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

2. 重写规则

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

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s [OR]

RewriteCond %{REQUEST_FILENAME} -l [OR]

RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ - [NC,L]

RewriteRule ^.*$ /index.php [NC,L]

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

3. 引导程序

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

<?php

define('ZFW_VERSION','1.10.1');

define('ZFW_PREFIX','C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/mydomain.com/library');

define('APP_PATH',realpath(dirname(_FILE_) . '/../'));

 

$paths=array(

  APP_PATH,

  APP_PATH.DIRECTORY_SEPARATOR.'application'.DIRECTORY_SEPARATOR.'models',

  ZFW_PREFIX.DIRECTORY_SEPARATOR.'ZendFramework-'.ZFW_VERSION.DIRECTORY_SEPARATOR.'library',

  get_include_path()

);

 

set_include_path(implode(PATH_SEPARATOR,$paths));

require_once('Zend/Loader.php');

 

Zend_Loader::registerAutoload();

$front=Zend_Controller_Front::getInstance();

$front->throwExceptions(true);

$front->run(APP_PATH.'/application/controllers');

?>

上面代码的作用是实例化前端控制器(Front Controller)并运行它。

4. 默认的动作控制器(Action Controller

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

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

转到 index 方法。

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

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

<?php

 

/**

* IndexController - The default controller class

*

* @author

* @version

*/

 

require_once 'Zend/Controller/Action.php';

 

//默认控制器类

class IndexController extends Zend_Controller_Action {

/**

* The default action - show the home page

*/

    public function indexAction() {

       //动作方法名:对应的文件是views/scripts/index/index.phtml

       $this->view->name='Kevin';     

    }

       

public function showAction() {

      //新增的动作方法名:对应的文件是views/scripts/index/show.phtml

   // TODO Auto-generated LoveController::indexAction() default action

}

 

}

 

?>

5. 视图(页面)脚本

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

< <!DOCTYPE html

 

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

 

"http://www.w3.org/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>

    <p>Welcome:<?php echo $this->name;?></p>

 

</body>

 

</html>

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

http://localhost/index.php

Hello, World!

Welcome:Kevin

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值