1. 安裝
從 Zend Framework 的網頁上下載最新版本。解壓后,把整個目錄拷貝到一個理想的地方,比如:/php/library/Zend。
打開 php.ini 文件,確認包含 Zend 目錄的路徑在 include_path 里定義了。以上面的配置為例,php.ini 中應有類似下面的條目:
include_path = ".:/php/library"
注意:Windows 下的寫法略有不同,應該類似于 include_path = ".;C:/php/library"
初始的安裝就這么簡單。Zend Framework 的一些組件會用到 php 的一些附加模塊。具體的要求請參考這里。
2. 項目的目錄結構
如果你的項目不包含多個模塊,可以用下面的目錄結構:
application/controllers/IndexController.phpmodels/views/scripts/index/index.phtmlhelpers/filters/html/.htaccessindex.php如果你的項目要包含多個模塊(比如:博客,社區,等等),那么建議使用模塊化的目錄結構。
3. 網頁的根目錄
網頁的根目錄應指向上述目錄結構中的 html 文件夾。
4. 重寫規則
編輯 html/.htaccess 文件,加入下面兩行:
RewriteEngine onRewriteRule !/.(js|ico|gif|jpg|png|CSS)$ index.php注意:上述是針對 apache 的配置。如果是其他的服務器,請參考這里。
5. 引導程序
編輯 html/index.php 文件,敲入下面代碼:
<?ph Zend Framework 的默認路由規則是 http://域名/控制器名/動作(方法)名。例如: http://example.com/user/show 會被解析到名為 User 的控制器以及該控制器中定義的 show 方法。如果該方法沒有定義,則默認轉到 index 方法。 注意:在代碼中,控制器名的后面要加上 Controller,而動作名的后面要加上 Action。 編輯 application/controllers/IndexController.php 文件,輸入: classIndexControllerextendsZend_Controller_Action 7. 視圖(頁面)腳本 編輯 application/views/scripts/index/index.phtml,輸入: <!DOCTYPE htmlPUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"" http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>My first Zend Framework App</title></head><body><h1>Hello, World!</h1></body></html>8. 錯誤控制器 默認情況下,Zend Framework 的錯誤處理插件是被注冊的。它需要一個錯誤控制器來處理錯誤。缺省的錯誤控制處理被假定為 ErrorController 以及其中定義的 errorAction。 編輯 application/controllers/ErrorController.php,輸入: classErrorControllerextendsZend_Controller_Action 下面是對應的視圖腳本。編輯 application/views/scripts/error/error.phtml,輸入: <!DOCTYPE htmlPUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"" http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Error</title></head><body><h1>An error occurred</h1><p>An error occurred; please try again later.</p></body></html>9. 運行 好,現在運行網站。在瀏覽器中鍵入下面三個地址,得到的結果應該是一樣的——就是最最常見的“Hello, World!“。 http://域名
<?php
/** Zend_Controller_Action */
require_once'Zend/Controller/Action.php';
{
public functionindexAction()
{
}
}
<?php
/** Zend_Controller_Action */
require_once'Zend/Controller/Action.php';
{
public functionerrorAction()
{
}
}
http://域名/index
http://域名/index/index
如果是這樣,那么恭喜你!
新聞熱點
疑難解答