@(未配置ZendFramwork的同窗,在個人博客裏:[ NetBeans配置zendFramework ],觀看如何配置) php
@ 到網上下載個 zendFramework的API (建議下載英文文檔)。 css
打開API,點擊 Learnint Zend Framework 路徑下的 html
- Zend Framework Quick Start sql
@ 根據裏面的教程走,就能建立一個‘留言板’。 數據庫
首先,咱們須要瞭解一下, 到底須要執行哪些步驟 bootstrap
① 建立數據庫表 |
: test數據庫下,建立guestbook表 |
② 配置 Bootstrap.php |
:"引導文件" |
③ 建立 layout |
:"佈局" |
④ 配置 applicatin.ini |
:"配置文件" :配置數據庫、佈局...信息 |
⑤ 建立model | :"模型",2部分:1實體bean; 2數據庫入口方法(select,insert,update,delete) |
⑥ 建立dbModel |
:"表關聯模型":指定模型映射哪個數據表 |
⑦ 建立 mapper |
:"模型入口實現" :模型的入口方法在這裏實現 |
⑧ 建立 Controller |
:"控制器":調用模型方法,獲值後傳遞給視圖 |
⑨ 建立 view |
:"顯示層phtml",PHP的HTML頁面 |
⑩ 演示 |
|
建立 layout、Controller的時候,必定得用 zend命令 建立,不要手動建立。 app
|
|
|
■ .zfproject.xml文件在項目裏看不到,獲得你的工做空間所在的項目下才能看到。裏面是關於Controller調用Action的配置 |
|
|
|
|
@ 打開命令窗口 如圖:↓ ide
use test; CREATE TABLE guestbook ( id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, email VARCHAR(32) NOT NULL DEFAULT 'noemail@test.com', comment TEXT NULL, created DATETIME NOT NULL ); CREATE INDEX id ON guestbook(id); INSERT INTO guestbook (email, comment, created) VALUES ('ralph.schindler@zend.com','Hello! Hope you enjoy this sample zf application!', now()); INSERT INTO guestbook (email, comment, created) VALUES ('foo@bar.com', 'Baz baz baz, baz baz Baz baz baz - baz baz baz.', now());
② 修改Bootstrap.php (引導文件) 佈局
./application/Bootstrap.php 將如下代碼替換進去↓ 測試
/** * 添加引導資源的最簡單方法是隻需用短語_init建立一個受保護的方法開始。在這種狀況下, * 咱們想要初始化 doctype,因此咱們將咱們引導類別內建立_initDoctype()的一種方法: */ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { protected function _initDoctype() { //初始化視圖 $this->bootstrap('view'); $view = $this->getResource('view'); $view->doctype('XHTML1_STRICT'); } }
@ 執行zend命令,建立layout。
過濾器: enable config
@ 執行後,自動建立一個layout.ptml,地址以下:
./application/layouts/scripts/layout.ptml 將如下代碼替換進去↓
<?php echo $this->doctype() ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Zend Framework Quickstart Application</title> <?php echo $this->headLink()->appendStylesheet('/css/global.css') ?> </head> <body> <div id="header" style="background-color: #EEEEEE; height: 30px;"> <div id="header-logo" style="float: left"> <b>ZF Quickstart Application</b> </div> <div id="header-navigation" style="float: right"> <a href="<?php echo $this->url( array('controller'=>'guestbook'), 'default', true) ?>">Guestbook</a> </div> </div> <?php echo $this->layout()->content ?> </body> </html>
④ 配置application.ini(配置數據庫)
./application/configs/application.ini 將如下代碼替換進去↓
[production] phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 includePaths.library = APPLICATION_PATH "/../library" bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap" appnamespace = "Application" resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" resources.frontController.params.displayExceptions = 0 ;佈局 ( zend命令建立layout的時候,自動生成這一段信息 ) resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/" ;數據庫 resources.db.adapter = PDO_MYSQL resources.db.params.host = localhost resources.db.params.username = root resources.db.params.password = ogepyj resources.db.params.dbname = test resources.db.params.charset = utf8 ;視圖 resources.view[] = [staging : production] [testing : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 [development : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 resources.frontController.params.displayExceptions = 1
@ 在如下路徑建立一個Guestbook.php
./application/models/Guestbook.php 將如下代碼替換進去↓
class Application_Model_Guestbook { protected $_comment; protected $_created; protected $_email; protected $_id; protected $_mapper; public function __construct(array $options = null) { if (is_array($options)) { $this->setOptions($options); } } public function __set($name, $value){ $method = 'set' . $name; if (('mapper' == $name) || !method_exists($this, $method)) { throw new Exception('Invalid guestbook property'); } $this->$method($value); } public function __get($name){ $method = 'get' . $name; if (('mapper' == $name) || !method_exists($this, $method)) { throw new Exception('Invalid guestbook property'); } return $this->$method(); } public function setOptions(array $options){ $methods = get_class_methods($this); foreach ($options as $key => $value) { $method = 'set' . ucfirst($key); if (in_array($method, $methods)) { $this->$method($value); } } return $this; } public function setComment($text){ $this->_comment = (string) $text; return $this; } public function getComment(){ return $this->_comment; } public function setEmail($email){ $this->_email = (string) $email; return $this; } public function getEmail(){ return $this->_email; } public function setCreated($ts){ $this->_created = $ts; return $this; } public function getCreated(){ return $this->_created; } public function setId($id){ $this->_id = (int) $id; return $this; } public function getId(){ return $this->_id; } public function setMapper($mapper){ $this->_mapper = $mapper; return $this; } public function getMapper(){ if (null === $this->_mapper) { $this->setMapper(new Application_Model_GuestbookMapper()); } return $this->_mapper; } public function save(){ $this->getMapper()->save($this); } public function find($id){ $this->getMapper()->find($id, $this); return $this; } public function fetchAll(){ return $this->getMapper()->fetchAll(); } }
@ 在如下位置建立 Guestbook.php (因爲DbTable文件夾不存在,因此得先建立)
./application/models/DbTable/Guestbook.php 將如下代碼替換進去↓
class Application_Model_DbTable_Guestbook extends Zend_Db_Table_Abstract { /** Table name */ protected $_name = 'guestbook'; }
./application/models/GuestbookMapper.php 將如下代碼替換進去↓
class Application_Model_GuestbookMapper { protected $_dbTable; public function setDbTable($dbTable) { if (is_string($dbTable)) { $dbTable = new $dbTable(); } if (!$dbTable instanceof Zend_Db_Table_Abstract) { throw new Exception('Invalid table data gateway provided'); } $this->_dbTable = $dbTable; return $this; } public function getDbTable(){ if (null === $this->_dbTable) { $this->setDbTable('Application_Model_DbTable_Guestbook'); } return $this->_dbTable; } public function save(Application_Model_Guestbook $guestbook){ $data = array( 'email' => $guestbook->getEmail(), 'comment' => $guestbook->getComment(), 'created' => date('Y-m-d H:i:s'), ); if (null === ($id = $guestbook->getId())) { unset($data['id']); $this->getDbTable()->insert($data); } else { $this->getDbTable()->update($data, array('id = ?' => $id)); } } public function find($id, Application_Model_Guestbook $guestbook){ $result = $this->getDbTable()->find($id); if (0 == count($result)) { return; } $row = $result->current(); $guestbook->setId($row->id) ->setEmail($row->email) ->setComment($row->comment) ->setCreated($row->created); } public function fetchAll(){ $resultSet = $this->getDbTable()->fetchAll(); $entries = array(); foreach ($resultSet as $row) { $entry = new Application_Model_Guestbook(); $entry->setId($row->id) ->setEmail($row->email) ->setComment($row->comment) ->setCreated($row->created) ->setMapper($this); $entries[] = $entry; } return $entries; } }
@ zend命令建立 GuestbookController.php
過濾器 :create controller
參數:Guestbook
執行後,GuestbookController.php的路徑以下:
./application/controllers/GuestbookController.php 將如下代碼替換進去↓
class GuestbookController extends Zend_Controller_Action { public function indexAction() { $guestbook = new Application_Model_Guestbook(); $this->view->entries = $guestbook->fetchAll(); } }
@ 在如下位置建立一個 index.phtml (因爲guestbook文件夾不存在,因此得先建立)
./application/views/scripts/guestbook/index.phtml 將如下代碼替換進去↓
<!-- application/views/scripts/guestbook/index.phtml --> <p><a href="<?php echo $this->url( array( 'controller' => 'guestbook', 'action' => 'sign' ), 'default', true) ?>">Sign Our Guestbook</a></p> Guestbook Entries: <br /> <dl> <?php foreach ($this->entries as $entry): ?> <dt><?php echo $this->escape($entry->email) ?></dt> <dd><?php echo $this->escape($entry->comment) ?></dd> <?php endforeach ?> </dl>
@輸入如下地址進行測試:(每一個人項目放的工做空間不同,以致於地址不同,測試的時候,要仔細對應)
http://localhost/zendFramework/zendtest/public
@ 在右側有個連接:Guestbook 點擊進去後,就是剛纔項目所作的效果
@ 項目效果界面↓