您的位置:首页 > 编程语言 > PHP开发

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

2010-03-17 18:11 302 查看
Zendframework安装,学习来源于思考,多看多想多做,总会有进步的,有成果的那一刻是多么激动啊.

Zend Framework安裝
安装环境:
Windows XP Professional(SP3)
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.conf之document 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 (这个是ZF的library,可从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
这个是indexAction的view,当执行indexAction时,预设会寻找相同的文件名,并render出页面內容
Controller的设定大概这样就完成了(细节可再参考ZF的Document或是其它高手们的Tutorial)

它则是会找IndexController里index这个action,然后会找index.phtml来render页面內容。基本上到这里,就把这个小小的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’;
}
这个是indexAction的view,当执行indexAction时,预设会寻找相同的文件名,并render出页面內容
Controller的设定大概这样就完成了(细节可再参考ZF的Document或是其它高手们的Tutorial)

它则是会找IndexController里index这个action,然后会找index.phtml来render页面內容。基本上到这里,就把这个小小的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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: