Zend Framework安裝
[b]安装环境:[/b] 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)
[b]一、基本设置:
[/b]1. 设定mod_rewrite
编辑httpd.conf
#LoadModule rewrite_module modules/mod_rewrite.so
如果前面的”#”字在的话,就把它拿掉吧(mod_rewrite的詳細資料,可參考apache网站)
2. 设定include_path
设定include path之目的,是为了方便在include类別时,省去输入长长一串位置的时间。
a) 可直接修改php.ini之设定
b) 或是于程序中动态加入set_include_path
参考网址:[url=http://tw2.php.net/set_include_path]http://tw2.php.net/set_include_path[/url]
3. 设定httpd.conf之document root
请参考下一段之目录架构,将document root指向/html,设定完之后,请重新启动Apache,并建议检查一下error.log看是否有错误的地方。
[b]二、Zend Framework设定[/b]
1.基本目录架构
|-/application
|-/controllers (MVC之C)
|-/models (MVC之M)
|-/views (MVC之V)
|-/filters
|-/helpers
|-/scripts
|-/html
|-/images (存放图片)
|-/scripts (存放script)
|-/styles (存放CSS)
|-.htaccess (配合url rewrite之资料)
|-index.php (bootstrap file)
|-/library
|-/Zend (这个是ZF的library,可从ZF网站下载)
2. 檔案设定
a)index.php(bootstrap file),可视个别情况修改
[php]
<?php
//Basic Config
error_reporting(E_ALL | E_STRICT); //设定Error Report的等级
date_default_timezone_set('Asia/Taipei'); //设定时区为台北
//Include path
define ('P_S', PATH_SEPARATOR);
set_include_path('.' .P_S .'../library' .P_S .'../application/models/' .P_S .get_include_path());
require_once'Zend/Loader.php';
Zend_Loader::registerAutoload();
//Controller
$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory('../application/controllers');
$frontController->dispatch();
?>
[/php]
b).htaccess设定:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
基本上这样设定完,就差不多把环境搭建好了,接下來,就是开始设定各別Controller的工作了。
[b]PS1:[/b]在windows系统下要做出.htaccess,可以直接用记事本来做储存的时候,选择「存储类型(T)」为「所有文件」,即可直接输入档名.htaccess而不会发生错误。
[b]PS2:[/b]其它目录也可加个.htaccess档來保护目录里的资料,內容为:deny from all。
[b]三、Controller设定
[/b] 先设定一个最基本的IndexController,架构参考上一段落
|-/application
|-/controllers (MVC之C)
|-[b]IndexController.php (Index的Controller) <-新增这个文件[/b]
|-/models (MVC之M)
|-/views (MVC之V)
|-/filters
|-/helpers
|-/scripts
|-/[b]index <--新增这个目录
[/b] |-[b]index.phtml <--新增这个文件
[/b] |-[b]happy.phtml <--新增这个文件
[/b]1. IndexController.php
[php]
<?php
require_once 'Zend/Controller/Action.php';
class IndexController extends Zend_Controller_Action{
public function indexAction(){
//可以在写index的Action
}
public function happyAction(){
//可以在这里写happy的Action
}
}
?>
[/php]
2. index.phtml & happy.phtml
这个是indexAction的view,当执行indexAction时,预设会寻找相同的文件名,并render出页面內容
Controller的设定大概这样就完成了(细节可再参考ZF的Document或是其它高手们的Tutorial)
接下来,打开browser,输入网址:
[url=http://127.0.0.1/]http://127.0.0.1/[/url] 或是 [url=http://127.0.0.1/index]http://127.0.0.1/index[/url] 这两个网址,它都会找IndexController里index这个action
然后会找index.phtml来render页面內容 [url=http://127.0.0.1/index/happy]http://127.0.0.1/index/happy[/url]
它则是会找IndexController里happy这个action,然后会找happy.phtml来render页面內容。基本上到这里,就把这个小小的MVC架构做出来了。
页:
[1]
