Zend Framework開發(fā)入門經(jīng)典教程
本文講述了Zend Framework開發(fā)入門相關(guān)知識(shí)點(diǎn)。分享給大家供大家參考,具體如下:
Zend Framework發(fā)布了!雖然仍處于開發(fā)初期,這個(gè)教程仍突出講解目前幾個(gè)最好的功能,并指導(dǎo)你完成一個(gè)簡(jiǎn)單程序的構(gòu)建。
Zend最早在社區(qū)里發(fā)布了ZF?;谕瑯拥南敕ǎ@個(gè)教程寫來(lái)用于展示ZF現(xiàn)有的功能。由于這個(gè)教程是在線發(fā)布,我將在ZF變化時(shí)對(duì)其進(jìn)行更新,以便盡可能有效。
要求
Zend Framework要求PHP5。為了更好利用本教程的代碼,你還需要Apache網(wǎng)頁(yè)服務(wù)器。因?yàn)槭痉冻绦颍ㄒ粋€(gè)新聞管理系統(tǒng))用到了mod_rewrite。
這個(gè)教程的代碼可以自由下載,所以你可以自己試一下。你可以從Brain Buld的網(wǎng)站下載到代碼:http://brainbulb.com/zend-framework-tutorial.tar.gz。
下載ZF
當(dāng)你開始這篇教程時(shí),你需要下載ZF的最新版本。你可以用瀏覽器手工從http://framework.zend.com/download選擇tar.gz或zip文件進(jìn)行下載,或者使用下列命令:
$ wget http://framework.zend.com/download/tgz $ tar -xvzf ZendFramework-0.1.2.tar.gz
提示:Zend計(jì)劃提供自有PEAR通道簡(jiǎn)化下載。
一旦你下載了預(yù)覽版,把library目錄放到方便的地方。在這個(gè)教程,我把library重命名為lib以便有個(gè)簡(jiǎn)潔的目錄結(jié)構(gòu):
app/
views/
controllers/
www/
.htaccess
index.php
lib/
www目錄是文檔根目錄,controllers和views目錄是以后會(huì)用到的空目錄,而lib目錄來(lái)自你下載的預(yù)覽版。
開始
我要介紹的第一個(gè)組件是Zend_Controller。從很多方面看,它為你開發(fā)的程序提供了基礎(chǔ),同時(shí)也部分決定了Zend Framework不只是個(gè)組件的集合。但是,你在用之前需要將所有的得到的請(qǐng)求都放到一個(gè)簡(jiǎn)單的PHP腳本。本教程用的是mod_rewrite。
用mod_rewrite自身是一種藝術(shù),但幸運(yùn)的是,這個(gè)特殊的任務(wù)特別簡(jiǎn)單。如果你對(duì)mod_rewrite或Apache的一般配置不熟悉,在文檔根目錄下創(chuàng)建一個(gè).htaccess文件,并添加以下內(nèi)容:
RewriteEngine on RewriteRule !/.(js|ico|gif|jpg|png|css)$ index.php
提示: Zend_Controller的一個(gè)TODO項(xiàng)目就是取消對(duì)mod_rewrite的依賴。為了提供一個(gè)預(yù)覽版的范例,本教程用了mod_rewrite。
如果你直接把這些內(nèi)容添加到httpd.conf,你必須重啟網(wǎng)頁(yè)服務(wù)器。但如果你用.htaccess文件,則什么都不必做。你可以放一些具體的文本到index.php并訪問任意路徑如/foo/bar做一下快速測(cè)試。如你的域名為example.org,則訪問http://example.org/foo/bar。
你還要設(shè)置ZF庫(kù)的路徑到include_path。你可以在php.ini設(shè)置,也可以直接在你的.htaccess文件放下列內(nèi)容:
php_value include_path "/path/to/lib"
Zend
Zend類包含了一些經(jīng)常使用的靜態(tài)方法的集合。下面是唯一一個(gè)你要手工添加的類:
<?php include 'Zend.php'; ?>
一旦你包含了Zend.php,你就已經(jīng)包含了Zend類的所有的類方法。用loadClass()就可以簡(jiǎn)單地加載其它類。例如,加載Zend_Controller_Front類:
<?php include 'Zend.php'; Zend::loadClass('Zend_Controller_Front'); ?>
include_path能理解loadclass()及ZF的組織和目錄結(jié)構(gòu)。我用它加載所有其它類。
Zend_Controller
使用這個(gè)controller非常直觀。事實(shí)上,我寫本教程時(shí)并沒有用到它豐富的文檔。
提示:文檔目前已經(jīng)可以在http://framework.zend.com/manual/zend.controller.html看到。
我一開始是用一個(gè)叫Zend_Controller_Front的front controller。為了理解它是怎么工作的,請(qǐng)把下列代碼放在你的index.php文件:
<?php include 'Zend.php'; Zend::loadClass('Zend_Controller_Front'); $controller = Zend_Controller_Front::getInstance(); $controller->setControllerDirectory('/path/to/controllers'); $controller->dispatch(); ?>
如果你更喜歡對(duì)象鏈結(jié),可以用以下代碼代替:
<?php include 'Zend.php'; Zend::loadClass('Zend_Controller_Front'); $controller = Zend_Controller_Front::getInstance() ->setControllerDirectory('/path/to/controllers') ->dispatch(); ?>
現(xiàn)在如果你訪問/foo/bar,會(huì)有錯(cuò)誤發(fā)生。沒錯(cuò)!它讓你知道發(fā)生了什么事。主要的問題是找不到IndexController.php文件。
在你創(chuàng)建這個(gè)文件之前,應(yīng)先理解一下ZF想讓你怎樣組織這些事情。ZF把訪問請(qǐng)求給拆分開來(lái)。假如訪問的是/foo/bar,則foo是controller,而bar是action。它們的默認(rèn)值都是index.
如果foo是controller,ZF就會(huì)去查找controllers目錄下的FooController.php文件。因?yàn)檫@個(gè)文件不存在,ZF就退回到IndexController.php。結(jié)果都沒有找到,就報(bào)錯(cuò)了。
接下來(lái),在controllers目錄創(chuàng)建IndexController.php文件(可以用setControllerDirectory()設(shè)置):
<?php Zend::loadClass('Zend_Controller_Action'); class IndexController extends Zend_Controller_Action { public function indexAction() { echo 'IndexController::indexAction()'; } } ?>
就如剛才說明的,IndexController類處理來(lái)自index controller或controller不存在的請(qǐng)求。indexAction()方法處理action為index的訪問。要記住的是index是controller和action的默認(rèn)值。如果你訪問/,/index或/index/index,indexAction()方法就會(huì)被執(zhí)行。 (最后面的斜杠并不會(huì)改變這個(gè)行為。) 而訪問其他任何資源只會(huì)導(dǎo)致出錯(cuò)。
在繼續(xù)做之前,還要在IndexController加上另外一個(gè)有用的類方法。不管什么時(shí)候訪問一個(gè)不存在的控制器,都要調(diào)用noRouteAction()類方法。例如,在FooController.php不存在的條件下,訪問/foo/bar就會(huì)執(zhí)行noRouteAction()。但是訪問/index/foo仍會(huì)出錯(cuò),因?yàn)閒oo是action,而不是controller.
將noRouteAction()添加到IndexController.php:
<?php Zend::loadClass('Zend_Controller_Action'); class IndexController extends Zend_Controller_Action { public function indexAction() { echo 'IndexController::indexAction()'; } public function noRouteAction() { $this->_redirect('/'); } } ?>
例子中使用$this->_redirect('/')來(lái)描述執(zhí)行noRouteAction()時(shí),可能發(fā)生的行為。這會(huì)將對(duì)不存在controllers的訪問重定向到根文檔(首頁(yè))。
現(xiàn)在創(chuàng)建FooController.php:
<?php Zend::loadClass('Zend_Controller_Action'); class FooController extends Zend_Controller_Action { public function indexAction() { echo 'FooController::indexAction()'; } public function barAction() { echo 'FooController::barAction()'; } } ?>
如果你再次訪問/foo/bar,你會(huì)發(fā)現(xiàn)執(zhí)行了barAction(),因?yàn)閎ar是action?,F(xiàn)在你不只支持了友好的URL,還可以只用幾行代碼就做得這么有條理??岚?!
你也可以創(chuàng)建一個(gè)__call()類方法來(lái)處理像/foo/baz這樣未定義的action。
<?php Zend::loadClass('Zend_Controller_Action'); class FooController extends Zend_Controller_Action { public function indexAction() { echo 'FooController::indexAction()'; } public function barAction() { echo 'FooController::barAction()'; } public function __call($action, $arguments) { echo 'FooController:__call()'; } } ?>
現(xiàn)在你只要幾行代碼就可以很好地處理用戶的訪問了,準(zhǔn)備好繼續(xù)。
Zend_View
Zend_View是一個(gè)用來(lái)幫助你組織好你的view邏輯的類。這對(duì)于模板-系統(tǒng)是不可知的,為了簡(jiǎn)單起見,本教程不使用模板。如果你喜歡的話,不妨用一下。
記住,現(xiàn)在所有的訪問都是由front controller進(jìn)行處理。因此應(yīng)用框架已經(jīng)存在了,另外也必須遵守它。為了展示Zend_View的一個(gè)基本應(yīng)用,將IndexController.php修改如下:
<?php Zend::loadClass('Zend_Controller_Action'); Zend::loadClass('Zend_View'); class IndexController extends Zend_Controller_Action { public function indexAction() { $view = new Zend_View(); $view->setScriptPath('/path/to/views'); echo $view->render('example.php'); } public function noRouteAction() { $this->_redirect('/'); } } ?>
在views目錄創(chuàng)建example.php文件:
<html> <head> <title>This Is an Example</title> </head> <body> <p>This is an example.</p> </body> </html>
現(xiàn)在,如果你訪問自己網(wǎng)站的根資源,你會(huì)看到example.php的內(nèi)容。這仍沒什么用,但你要清楚你要在以一種結(jié)構(gòu)和組織非常清楚的方式在開發(fā)網(wǎng)絡(luò)應(yīng)用。
為了讓Zend_View的應(yīng)用更清楚一點(diǎn),,修改你的模板(example.php)包含以下內(nèi)容:
<html> <head> <title><?php echo $this->escape($this->title); ?></title> </head> <body> <?php echo $this->escape($this->body); ?> </body> </html>
現(xiàn)在已經(jīng)添加了兩個(gè)功能。$this->escape()類方法用于所有的輸出。即使你自己創(chuàng)建輸出,就像這個(gè)例子一樣。避開所有輸出也是一個(gè)很好的習(xí)慣,它可以在默認(rèn)情況下幫助你防止跨站腳本攻擊(XSS)。
$this->title和$this->body屬性用來(lái)展示動(dòng)態(tài)數(shù)據(jù)。這些也可以在controller中定義,所以我們修改IndexController.php以指定它們:
<?php Zend::loadClass('Zend_Controller_Action'); Zend::loadClass('Zend_View'); class IndexController extends Zend_Controller_Action { public function indexAction() { $view = new Zend_View(); $view->setScriptPath('/path/to/views'); $view->title = 'Dynamic Title'; $view->body = 'This is a dynamic body.'; echo $view->render('example.php'); } public function noRouteAction() { $this->_redirect('/'); } } ?>
現(xiàn)在你再次訪問根目錄,應(yīng)該就可以看到模板所使用的這些值了。因?yàn)槟阍谀0逯惺褂玫?this就是在Zend_View范圍內(nèi)所執(zhí)行的實(shí)例。
要記住example.php只是一個(gè)普通的PHP腳本,所以你完全可以做你想做的。只是應(yīng)努力只在要求顯示數(shù)據(jù)時(shí)才使用模板。你的controller (controller分發(fā)的模塊)應(yīng)處理你全部的業(yè)務(wù)邏輯。
在繼續(xù)之前,我想做最后一個(gè)關(guān)于Zend_View的提示。在controller的每個(gè)類方法內(nèi)初始化$view對(duì)象需要額外輸入一些內(nèi)容,而我們的主要目標(biāo)是讓快速開發(fā)網(wǎng)絡(luò)應(yīng)用更簡(jiǎn)單。如果所有模板都放在一個(gè)目錄下,是否要在每個(gè)例子中都調(diào)用setScriptPath()也存在爭(zhēng)議。
幸運(yùn)的是,Zend類包含了一個(gè)寄存器來(lái)幫助減少工作量。你可以用register()方法把你的$view對(duì)象存儲(chǔ)在寄存器:
<?php Zend::register('view', $view); ?>
用registry()方法進(jìn)行檢索:
<?php $view = Zend::registry('view'); ?>
基于這點(diǎn),本教程使用寄存器。
Zend_InputFilter
本教程討論的最后一個(gè)組件是Zend_InputFilter。這個(gè)類提供了一種簡(jiǎn)單而有效的輸入過濾方法。你可以通過提供一組待過濾數(shù)據(jù)來(lái)進(jìn)行初始化。
<?php $filterPost = new Zend_InputFilter($_POST); ?>
這會(huì)將($_POST)設(shè)置為NULL,所以就不能直接進(jìn)入了。Zend_InputFilter提供了一個(gè)簡(jiǎn)單、集中的根據(jù)特定規(guī)則過濾數(shù)據(jù)的類方法集。例如,你可以用getAlpha()來(lái)獲取$_POST['name']中的字母:
<?php /* $_POST['name'] = 'John123Doe'; */ $filterPost = new Zend_InputFilter($_POST); /* $_POST = NULL; */ $alphaName = $filterPost->getAlpha('name'); /* $alphaName = 'JohnDoe'; */ ?>
每一個(gè)類方法的參數(shù)都是對(duì)應(yīng)要過濾的元素的關(guān)鍵詞。對(duì)象(例子中的$filterPost)可以保護(hù)數(shù)據(jù)不被篡改,并能更好地控制對(duì)數(shù)據(jù)的操作及一致性。因此,當(dāng)你操縱輸入數(shù)據(jù),應(yīng)始終使用Zend_InputFilter。
提示:Zend_Filter提供與Zend_InputFilter方法一樣的靜態(tài)方法。
構(gòu)建新聞管理系統(tǒng)
雖然預(yù)覽版提供了許多組件(甚至許多已經(jīng)被開發(fā)),我們已經(jīng)討論了構(gòu)建一個(gè)簡(jiǎn)單程序所需要的全部組件。在這里,你會(huì)對(duì)ZF的基本結(jié)構(gòu)和設(shè)計(jì)有更清楚的理解。
每個(gè)人開發(fā)的程序都會(huì)有所不同,而Zend Framework試圖包容這些差異。同樣,這個(gè)教程是根據(jù)我的喜好寫的,請(qǐng)根據(jù)自己的偏好自行調(diào)整。
當(dāng)我開發(fā)程序時(shí),我會(huì)先做界面。這并不意味著我把時(shí)間都花在標(biāo)簽、樣式表和圖片上,而是我從一個(gè)用戶的角度去考慮問題。因此我把程序看成是頁(yè)面的集合,每一頁(yè)都是一個(gè)獨(dú)立的網(wǎng)址。這個(gè)新聞系統(tǒng)就是由以下網(wǎng)址組成的:
/
/add/news
/add/comment
/admin
/admin/approve
/view/{id}
你可以直接把這些網(wǎng)址和controller聯(lián)系起來(lái)。IndexController列出新聞,AddController添加新聞和評(píng)論,AdminController處理一些如批準(zhǔn)新聞之類的管理,ViewController特定新聞和對(duì)應(yīng)評(píng)論的顯示。
如果你的FooController.php還在,把它刪除。修改IndexController.php,為業(yè)務(wù)邏輯以添加相應(yīng)的action和一些注釋:
<?php Zend::loadClass('Zend_Controller_Action'); class IndexController extends Zend_Controller_Action { public function indexAction() { /* List the news. */ } public function noRouteAction() { $this->_redirect('/'); } } ?>
接下來(lái),創(chuàng)建AddController.php文件:
<?php Zend::loadClass('Zend_Controller_Action'); class AddController extends Zend_Controller_Action { function indexAction() { $this->_redirect('/'); } function commentAction() { /* Add a comment. */ } function newsAction() { /* Add news. */ } function __call($action, $arguments) { $this->_redirect('/'); } } ?>
記住AddController的indexAction()方法不能調(diào)用。當(dāng)訪問/add時(shí)會(huì)執(zhí)行這個(gè)類方法。因?yàn)橛脩艨梢允止ぴL問這個(gè)網(wǎng)址,這是有可能的,所以你要把用戶重定向到主頁(yè)、顯示錯(cuò)誤或你認(rèn)為合適的行為。
接下來(lái),創(chuàng)建AdminController.php文件:
<?php Zend::loadClass('Zend_Controller_Action'); class AdminController extends Zend_Controller_Action { function indexAction() { /* Display admin interface. */ } function approveAction() { /* Approve news. */ } function __call($action, $arguments) { $this->_redirect('/'); } } ?>
最后,創(chuàng)建ViewController.php文件:
<?php Zend::loadClass('Zend_Controller_Action'); class ViewController extends Zend_Controller_Action { function indexAction() { $this->_redirect('/'); } function __call($id, $arguments) { /* Display news and comments for $id. */ } } ?>
和AddController一樣,index()方法不能調(diào)用,所以你可以使用你認(rèn)為合適的action。ViewController和其它的有點(diǎn)不同,因?yàn)槟悴恢朗裁床攀怯行У腶ction。為了支持像/view/23這樣的網(wǎng)址,你要使用__call()來(lái)支持動(dòng)態(tài)action。
數(shù)據(jù)庫(kù)操作
因?yàn)閆end Framework的數(shù)據(jù)庫(kù)組件還不穩(wěn)定,而我希望這個(gè)演示可以做得簡(jiǎn)單一點(diǎn)。我使用了一個(gè)簡(jiǎn)單的類,用SQLite進(jìn)行新聞條目和評(píng)論的存儲(chǔ)和查詢。
<?php class Database { private $_db; public function __construct($filename) { $this->_db = new SQLiteDatabase($filename); } public function addComment($name, $comment, $newsId) { $name = sqlite_escape_string($name); $comment = sqlite_escape_string($comment); $newsId = sqlite_escape_string($newsId); $sql = "INSERT INTO comments (name, comment, newsId) VALUES ('$name', '$comment', '$newsId')"; return $this->_db->query($sql); } public function addNews($title, $content) { $title = sqlite_escape_string($title); $content = sqlite_escape_string($content); $sql = "INSERT INTO news (title, content) VALUES ('$title', '$content')"; return $this->_db->query($sql); } public function approveNews($ids) { foreach ($ids as $id) { $id = sqlite_escape_string($id); $sql = "UPDATE news SET approval = 'T' WHERE id = '$id'"; if (!$this->_db->query($sql)) { return FALSE; } } return TRUE; } public function getComments($newsId) { $newsId = sqlite_escape_string($newsId); $sql = "SELECT name, comment FROM comments WHERE newsId = '$newsId'"; if ($result = $this->_db->query($sql)) { return $result->fetchAll(); } return FALSE; } public function getNews($id = 'ALL') { $id = sqlite_escape_string($id); switch ($id) { case 'ALL': $sql = "SELECT id, title FROM news WHERE approval = 'T'"; break; case 'NEW': $sql = "SELECT * FROM news WHERE approval != 'T'"; break; default: $sql = "SELECT * FROM news WHERE id = '$id'"; break; } if ($result = $this->_db->query($sql)) { if ($result->numRows() != 1) { return $result->fetchAll(); } else { return $result->fetch(); } } return FALSE; } } ?>
(你可以用自己的解決方案隨意替換這個(gè)類。這里只是為你提供一個(gè)完整示例的介紹,并非建議要這么實(shí)現(xiàn)。)
這個(gè)類的構(gòu)造器需要SQLite數(shù)據(jù)庫(kù)的完整路徑和文件名,你必須自己進(jìn)行創(chuàng)建。
<?php $db = new SQLiteDatabase('/path/to/db.sqlite'); $db->query("CREATE TABLE news ( id INTEGER PRIMARY KEY, title VARCHAR(255), content TEXT, approval CHAR(1) DEFAULT 'F' )"); $db->query("CREATE TABLE comments ( id INTEGER PRIMARY KEY, name VARCHAR(255), comment TEXT, newsId INTEGER )"); ?>
你只需要做一次,以后直接給出Database類構(gòu)造器的完整路徑和文件名即可:
<?php $db = new Database('/path/to/db.sqlite'); ?>
整合
為了進(jìn)行整合,在lib目錄下創(chuàng)建Database.php,loadClass()就可以找到它。你的index.php文件現(xiàn)在就會(huì)初始化$view和$db并存儲(chǔ)到寄存器。你也可以創(chuàng)建__autoload()函數(shù)自動(dòng)加載你所需要的類:
<?php include 'Zend.php'; function __autoload($class) { Zend::loadClass($class); } $db = new Database('/path/to/db.sqlite'); Zend::register('db', $db); $view = new Zend_View; $view->setScriptPath('/path/to/views'); Zend::register('view', $view); $controller = Zend_Controller_Front::getInstance() ->setControllerDirectory('/path/to/controllers') ->dispatch(); ?>
接下來(lái),在views目錄創(chuàng)建一些簡(jiǎn)單的模板。index.php可以用來(lái)顯示index視圖:
<html> <head> <title>News</title> </head> <body> <h1>News</h1> <?php foreach ($this->news as $entry) { ?> <p> <a href="/view/<?php echo $this->escape($entry['id']); ?>"> <?php echo $this->escape($entry['title']); ?> </a> </p> <?php } ?> <h1>Add News</h1> <form action="/add/news" method="POST"> <p>Title:<br /><input type="text" name="title" /></p> <p>Content:<br /><textarea name="content"></textarea></p> <p><input type="submit" value="Add News" /></p> </form> </body> </html>
view.php模板可以用來(lái)顯示選定的新聞條目:
<html> <head> <title> <?php echo $this->escape($this->news['title']); ?> </title> </head> <body> <h1> <?php echo $this->escape($this->news['title']); ?> </h1> <p> <?php echo $this->escape($this->news['content']); ?> </p> <h1>Comments</h1> <?php foreach ($this->comments as $comment) { ?> <p> <?php echo $this->escape($comment['name']); ?> writes: </p> <blockquote> <?php echo $this->escape($comment['comment']); ?> </blockquote> <?php } ?> <h1>Add a Comment</h1> <form action="/add/comment" method="POST"> <input type="hidden" name="newsId" value="<?php echo $this->escape($this->id); ?>" /> <p>Name:<br /><input type="text" name="name" /></p> <p>Comment:<br /><textarea name="comment"></textarea></p> <p><input type="submit" value="Add Comment" /></p> </form> </body> </html>
最后,admin.php模板可以用來(lái)批準(zhǔn)新聞條目:
<html> <head> <title>News Admin</title> </head> <body> <form action="/admin/approve" method="POST"> <?php foreach ($this->news as $entry) { ?> <p> <input type="checkbox" name="ids[]" value="<?php echo $this->escape($entry['id']); ?>" /> <?php echo $this->escape($entry['title']); ?> <?php echo $this->escape($entry['content']); ?> </p> <?php } ?> <p> Password:<br /><input type="password" name="password" /> </p> <p><input type="submit" value="Approve" /></p> </form> </body> </html>
提示:為了保持簡(jiǎn)單,這個(gè)表單用密碼作為驗(yàn)證機(jī)制。
使用到模板的地方,你只需要把注釋替換成幾行代碼。如IndexController.php就變成下面這樣:
<?php class IndexController extends Zend_Controller_Action { public function indexAction() { /* List the news. */ $db = Zend::registry('db'); $view = Zend::registry('view'); $view->news = $db->getNews(); echo $view->render('index.php'); } public function noRouteAction() { $this->_redirect('/'); } } ?>
因?yàn)闂l理比較清楚,這個(gè)程序首頁(yè)的整個(gè)業(yè)務(wù)邏輯只有四行代碼。AddController.php更復(fù)雜一點(diǎn),它需要更多的代碼:
<?php class AddController extends Zend_Controller_Action { function indexAction() { $this->_redirect('/'); } function commentAction() { /* Add a comment. */ $filterPost = new Zend_InputFilter($_POST); $db = Zend::registry('db'); $name = $filterPost->getAlpha('name'); $comment = $filterPost->noTags('comment'); $newsId = $filterPost->getDigits('newsId'); $db->addComment($name, $comment, $newsId); $this->_redirect("/view/$newsId"); } function newsAction() { /* Add news. */ $filterPost = new Zend_InputFilter($_POST); $db = Zend::registry('db'); $title = $filterPost->noTags('title'); $content = $filterPost->noTags('content'); $db->addNews($title, $content); $this->_redirect('/'); } function __call($action, $arguments) { $this->_redirect('/'); } } ?>
因?yàn)橛脩粼谔峤槐韱魏蟊恢囟ㄏ颍@個(gè)controller不需要視圖。
在AdminController.php,你要處理顯示管理界面和批準(zhǔn)新聞兩個(gè)action:
<?php class AdminController extends Zend_Controller_Action { function indexAction() { /* Display admin interface. */ $db = Zend::registry('db'); $view = Zend::registry('view'); $view->news = $db->getNews('NEW'); echo $view->render('admin.php'); } function approveAction() { /* Approve news. */ $filterPost = new Zend_InputFilter($_POST); $db = Zend::registry('db'); if ($filterPost->getRaw('password') == 'mypass') { $db->approveNews($filterPost->getRaw('ids')); $this->_redirect('/'); } else { echo 'The password is incorrect.'; } } function __call($action, $arguments) { $this->_redirect('/'); } } ?>
最后是ViewController.php:
<?php class ViewController extends Zend_Controller_Action { function indexAction() { $this->_redirect('/'); } function __call($id, $arguments) { /* Display news and comments for $id. */ $id = Zend_Filter::getDigits($id); $db = Zend::registry('db'); $view = Zend::registry('view'); $view->news = $db->getNews($id); $view->comments = $db->getComments($id); $view->id = $id; echo $view->render('view.php'); } } ?>
雖然很簡(jiǎn)單,但我們還是提供了一個(gè)功能較全的新聞和評(píng)論程序。最好的地方是由于有較好的設(shè)計(jì),增加功能變得很簡(jiǎn)單。而且隨著Zend Framework越來(lái)越成熟,只會(huì)變得更好。
更多信息
這個(gè)教程只是討論了ZF表面的一些功能,但現(xiàn)在也有一些其它的資源可供參考。在http://framework.zend.com/manual/有手冊(cè)可以查詢,Rob Allen在http://akrabat.com/zend-framework/介紹了一些他使用Zend Framework的經(jīng)驗(yàn),而Richard Thomas也在http://www.cyberlot.net/zendframenotes提供了一些有用的筆記。如果你有自己的想法,可以訪問Zend Framework的新論壇:http://www.phparch.com/discuss/index.php/f/289//。
結(jié)束語(yǔ)
要對(duì)預(yù)覽版進(jìn)行評(píng)價(jià)是很容易的事,我在寫這個(gè)教程時(shí)也遇到很多困難??偟膩?lái)說,我想Zend Framework顯示了承諾,加入的每個(gè)人都是想繼續(xù)完善它。
更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《Yii框架入門及常用技巧總結(jié)》、《ThinkPHP入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Zend Framework框架的PHP程序設(shè)計(jì)有所幫助。
- Zend Framework 2.0事件管理器(The EventManager)入門教程
- Zend Framework數(shù)據(jù)庫(kù)操作技巧總結(jié)
- Zend Framework數(shù)據(jù)庫(kù)操作方法實(shí)例總結(jié)
- Zend Framework入門教程之Zend_Db數(shù)據(jù)庫(kù)操作詳解
- ZendFramework框架實(shí)現(xiàn)連接兩個(gè)或多個(gè)數(shù)據(jù)庫(kù)的方法
- Zend Framework教程之連接數(shù)據(jù)庫(kù)并執(zhí)行增刪查的方法(附demo源碼下載)
- Zend Framework連接Mysql數(shù)據(jù)庫(kù)實(shí)例分析
- 解析如何使用Zend Framework 連接數(shù)據(jù)庫(kù)
- zend framework配置操作數(shù)據(jù)庫(kù)實(shí)例分析
- windows下zendframework項(xiàng)目環(huán)境搭建(通過命令行配置)
- zend framework多模塊多布局配置
- ZendFramework2連接數(shù)據(jù)庫(kù)操作實(shí)例
相關(guān)文章
Laravel 6 將新增為指定隊(duì)列任務(wù)設(shè)置中間件的功能
這篇文章主要介紹了Laravel 6 將新增為指定隊(duì)列任務(wù)設(shè)置中間件的功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08php中配置文件保存修改操作 如config.php文件的讀取修改等操作
有時(shí)候我們需要用php將一些配置參數(shù)寫到文件里面,方便后期讀取使用,這里就為大家分享一下具體的實(shí)現(xiàn)代碼,需要的朋友可以參考一下2021-05-05PHP判斷是手機(jī)端還是PC端 PHP判斷是否是微信瀏覽器
這篇文章主要為大家詳細(xì)介紹了PHP判斷是手機(jī)端還是PC端,以及PHP判斷是否是微信瀏覽器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03PHP以json或xml格式返回請(qǐng)求數(shù)據(jù)的方法
今天小編就為大家分享一篇PHP以json或xml格式返回請(qǐng)求數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-05-05在IIS7.0下面配置PHP 5.3.2運(yùn)行環(huán)境的方法
最近心血來(lái)潮,想學(xué)習(xí)一下php,既然想學(xué)習(xí)了就得需要搭環(huán)境。在網(wǎng)上找來(lái)找去都是說IIS5.0或者6.0的配置。真是看得云里霧里的,這樣直接影響了我的判斷力?,F(xiàn)特意寫下來(lái)在IIS7.0下面如何進(jìn)行配置PHP。2010-04-04Thinkphp5+plupload實(shí)現(xiàn)的圖片上傳功能示例【支持實(shí)時(shí)預(yù)覽】
這篇文章主要介紹了Thinkphp5+plupload實(shí)現(xiàn)的圖片上傳功能,結(jié)合具體實(shí)例形式分析了thinkPHP5結(jié)合plupload實(shí)現(xiàn)可支持實(shí)時(shí)預(yù)覽的圖片上傳功能相關(guān)操作技巧,需要的朋友可以參考下2019-05-05php把時(shí)間戳轉(zhuǎn)換成多少時(shí)間之前函數(shù)的實(shí)例
下面小編就為大家?guī)?lái)一篇php把時(shí)間戳轉(zhuǎn)換成多少時(shí)間之前函數(shù)的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-11-11laravel 錯(cuò)誤處理,接口錯(cuò)誤返回json代碼
今天小編就為大家分享一篇laravel 錯(cuò)誤處理,接口錯(cuò)誤返回json代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-10-10- php5.3是php5的一個(gè)里程碑,加入了大量新特性。本文主要給大家介紹了php5.3的一個(gè)新功能--延遲靜態(tài)綁定,希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2016-01-01
最新評(píng)論