国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

Zend Framework 入門――快速上手

2024-05-04 21:51:51
字體:
來源:轉載
供稿:網友

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.php
models/
views/
scripts/
index/
index.phtml
helpers/
filters/
html/
.htaccess
index.php

如果你的項目要包含多個模塊(比如:博客,社區,等等),那么建議使用模塊化的目錄結構。

3. 網頁的根目錄

網頁的根目錄應指向上述目錄結構中的 html 文件夾。

4. 重寫規則

編輯 html/.htaccess 文件,加入下面兩行:

RewriteEngine onRewriteRule !/.(js|ico|gif|jpg|png|css)$ index.php
注意:上述是針對 apache 的配置。如果是其他的服務器,請參考這里。

5. 引導程序

編輯 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 的默認路由規則是 http://域名/控制器名/動作(方法)名。例如:

http://example.com/user/show 會被解析到名為 User 的控制器以及該控制器中定義的 show 方法。如果該方法沒有定義,則默認轉到 index 方法。

注意:在代碼中,控制器名的后面要加上 Controller,而動作名的后面要加上 Action。

編輯 application/controllers/IndexController.php 文件,輸入:

<?php

/** Zend_Controller_Action */

require_once 'Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
}
}

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
{
public function errorAction()
{
}
}

下面是對應的視圖腳本。編輯 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. 運行

好,現在運行網站。在瀏覽器中鍵入下面三個地址,得到的結果應該是一樣的——就是最最常見的“Hello, World!“。

http://域名
http://域名/index
http://域名/index/index
如果是這樣,那么恭喜你!

相關文章

Zend Framework 入門——快速上手

Zend Framework 入門——多國語言支持

Zend Framework 入門——錯誤處理

Zend Framework 入門——頁面布局

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沅陵县| 随州市| 儋州市| 博爱县| 涞水县| 沁源县| 清苑县| 抚顺市| 长岛县| 龙川县| 双江| 老河口市| 丽江市| 集安市| 武城县| 清水河县| 区。| 海宁市| 正阳县| 南川市| 铜川市| 景宁| 赤水市| 延安市| 云阳县| 建阳市| 体育| 剑阁县| 浙江省| 固安县| 琼海市| 云浮市| 乾安县| 沐川县| 武陟县| 临汾市| 台中县| 民勤县| 响水县| 庄河市| 光山县|