Zend Framework 入門——快速上手_PHP教程
推薦:你或許尚未了解PHP的那10件事情1.使用 ip2long() 和 long2ip() 函數(shù)來把 IP 地址轉(zhuǎn)化成整型存儲到數(shù)據(jù)庫里。這種方法把存儲空間降到了接近四分之一(char(15) 的 15 個字節(jié)對整形的 4 個字節(jié)),計算一個特定的地址是不是在一
1. 安裝
從 Zend Framework 的網(wǎng)頁上下載最新版本。解壓后,把整個目錄拷貝到一個理想的地方,比如:/php/library/Zend。
打開 php.ini 文件,確認包含 Zend 目錄的路徑在 include_path 里定義了。以上面的配置為例,php.ini 中應(yīng)有類似下面的條目:
include_path = ".:/php/library"
注意:Windows 下的寫法略有不同,應(yīng)該類似于 include_path = ".;C:\php\library"
初始的安裝就這么簡單。Zend Framework 的一些組件會用到 php 的一些附加模塊。具體的要求請參考這里。
2. 項目的目錄結(jié)構(gòu)
如果你的項目不包含多個模塊,可以用下面的目錄結(jié)構(gòu):
application/
controllers/
IndexController.php
models/
views/
scripts/
index/
index.phtml
helpers/
filters/
html/
.htaccess
index.php
如果你的項目要包含多個模塊(比如:博客,社區(qū),等等),那么建議使用模塊化的目錄結(jié)構(gòu)。
3. 網(wǎng)頁的根目錄
網(wǎng)頁的根目錄應(yīng)指向上述目錄結(jié)構(gòu)中的 html 文件夾。
4. 重寫規(guī)則
編輯 html/.htaccess 文件,加入下面兩行:
RewriteEngine onRewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
注意:上述是針對 apache 的配置。如果是其他的服務(wù)器,請參考這里。
5. 引導(dǎo)程序
編輯 html/index.php 文件,敲入下面代碼:
<?php require_once 'Zend/Controller/Front.php'; $rootPath = dirname(dirname(__FILE__)); Zend_Controller_Front::run($rootPath . '/application/controllers'); |
上面代碼的作用是實例化前端控制器(Front Controller)并運行它。
6. 默認的動作控制器(Action Controller)
Zend Framework 的默認路由規(guī)則是 http://域名/控制器名/動作(方法)名。例如:
http://example.com/user/show 會被解析到名為 User 的控制器以及該控制器中定義的 show 方法。如果該方法沒有定義,則默認轉(zhuǎn)到 index 方法。
注意:在代碼中,控制器名的后面要加上 Controller,而動作名的后面要加上 Action。
編輯 application/controllers/IndexController.php 文件,輸入:
<?php /** Zend_Controller_Action */ require_once 'Zend/Controller/Action.php'; class IndexController extends Zend_Controller_Action |
7. 視圖(頁面)腳本
編輯 application/views/scripts/index/index.phtml,輸入:
<!DOCTYPE htmlPUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta 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,輸入:
<?php /** Zend_Controller_Action */ require_once 'Zend/Controller/Action.php'; class ErrorController extends Zend_Controller_Action |
下面是對應(yīng)的視圖腳本。編輯 application/views/scripts/error/error.phtml,輸入:
<!DOCTYPE htmlPUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta 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. 運行
好,現(xiàn)在運行網(wǎng)站。在瀏覽器中鍵入下面三個地址,得到的結(jié)果應(yīng)該是一樣的——就是最最常見的“Hello, World!“。
http://域名
http://域名/index
http://域名/index/index
如果是這樣,那么恭喜你!
相關(guān)文章
Zend Framework 入門——快速上手
Zend Framework 入門——多國語言支持
Zend Framework 入門——錯誤處理
Zend Framework 入門——頁面布局
分享:如何用PHP和mysql創(chuàng)建一個ShoutBox作為一個PHP開發(fā)人員,我有時被要求作個shoutbox 。 如果同樣的事情也發(fā)生在你身上,這里有一個快速指南。顯然,您要為它添加您自己的CSS在上面,但這里是基本思路。我們需要一個MySQL數(shù)據(jù)庫表
- 相關(guān)鏈接:
- 教程說明:
PHP教程-Zend Framework 入門——快速上手。