[心得] Zend Framework安裝
Zend Framework安裝
URL:
http://www.eddie.com.tw/blog/?p=103
環境:
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類別時,省去輸入長長一串位置的時間
a) 可直接修改php.ini之設定
b) 或是於程式中動態加入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. 檔案設定
a)index.php(bootstrap file),可視各別情況修改
<?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();
?>
b).htaccess設定:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
基本上這樣設定完,就差不多把環境建置好了
接下來,就是開始設定各別Controller的工作了
PS1:在windows系統下要做出.htaccess,可以直接用記事本來做
存檔的時候,選擇「存檔類型(T)」為「所有檔案」,即可直接輸入檔名.htaccess而不
會發生錯誤
PS2:其它目錄也可加個.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 <--新增這個檔案
a) IndexController.php
<?php
require_once 'Zend/Controller/Action.php';
class IndexController extends Zend_Controller_Action{
public function indexAction(){
//可以在寫index的Action
}
public function happyAction(){
//可以在這裡寫happy的Action
}
}
?>
b) 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,難免有些地方寫得不好,還請多指教 :)
程式碼有需要斷行的地方,再麻煩自己接起來囉 :)
--
http://www.eddie.com.tw/blog/
http://www.eddie.com.tw/
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.172.127
推
08/14 02:16, , 1F
08/14 02:16, 1F
推
08/14 09:11, , 2F
08/14 09:11, 2F
推
08/14 09:29, , 3F
08/14 09:29, 3F
推
08/14 09:39, , 4F
08/14 09:39, 4F
推
08/14 10:48, , 5F
08/14 10:48, 5F
推
08/14 14:05, , 6F
08/14 14:05, 6F
→
08/14 14:21, , 7F
08/14 14:21, 7F
※ 編輯: aquarianboy 來自: 220.133.136.167 (08/14 14:37)
推
08/14 14:37, , 8F
08/14 14:37, 8F
→
08/14 19:16, , 9F
08/14 19:16, 9F
→
08/14 19:33, , 10F
08/14 19:33, 10F
推
08/16 08:54, , 11F
08/16 08:54, 11F
PHP 近期熱門文章
PTT數位生活區 即時熱門文章