環境:
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)
基本設定
1. 設定mod_rewrite:
編輯httpd.conf:
#LoadModule rewrite_module modules/mod_rewrite.so
如果前面的”#”字在的話,就把它拿掉吧(mod_rewrite的詳細資料,可參考apache網站)。
2. 設定include_path:
設定include path之目的,是為了方便在include類別時,省去輸入長長一串位置的時間
可直接修改php.ini之設定。
或是於程式中動態加入set_include_path。
參考網址:http://tw2.php.net/set_include_path
3. 設定httpd.conf之document root:
請參考下一段之目錄架構,將document root指向/html。
以上設定完之後,請重新啟動Apache,並建議檢視一下error log,看是否有錯誤的地方
Zend Framework設定
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. 檔案設定
index.php(bootstrap file),可視各別情況修改:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<? 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 ();
.htaccess設定:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
基本上這樣設定完,就差不多把環境建置好了。接下來,就是開始設定各別Controller的工作了
其它
在windows系統下要做出.htaccess,可以直接用記事本來做。存檔的時候,選擇「存檔類型(T)」為「所有檔案」,即可直接輸入檔名.htaccess而不會發生錯誤。
其它目錄也可加個.htaccess檔來保護目錄裡的資料,內容為:
deny from all
Controller設定
先設定一個最基本的IndexController,架構請參考上一段:
|-/application
-|-/controllers (MVC之C)
–|-IndexController.php (Index的Controller) <-新增這個
-|-/models (MVC之M)
-|-/views (MVC之V)
--|-/filters
--|-/helpers
--|-/scripts
--|-/index <--新增這個目錄
----|-index.phtml <--新增這個檔案
----|-happy.phtml <--新增這個檔案
IndexController.php
1
2
3
4
5
6
7
8
9
10
11
<? php
require_once 'Zend/Controller/Action.php' ;
class IndexController extends Zend_Controller_Action {
public function indexAction (){
//可以在寫index的Action
}
public function happyAction (){
//可以在這裡寫happy的Action
}
}
index.phtml & happy.phtml
這個是indexAction的view,當執行indexAction時,預設會找同名名檔案,並render出頁面內容。Controller的設定大概這樣就完成了(細節可再參觀ZF的Document或是其它高手們的Tutorial)。接下來,打開browser,輸入網址:
http://127.0.0.1/
或是
http://127.0.0.1/index
這兩個網址,它都會找IndexController裡index這個action,然後會找index.phtml來render頁面內容。
http://127.0.0.1/index/happy
它則是會找IndexController裡happy這個action,然後會找happy.phtml來render頁面內容。
基本上到這裡,就把這個小小的MVC架構做出來了。
下一回,再來寫個自己做的簡單的通訊錄的CRUD(Create, Read, Update, Delete)
初試PHP的Framework,難免有些地方寫得不好,還請多指教 :)