由於yaf只是一個web框架,只負責處理web請求之類的基本功能,相當簡潔,連db庫都沒有。於是試着把zend 2.2的db庫,form庫,validator庫與yaf結合,寫了一個demo。因為zend 2.2框架的命名空間跟yaf一樣,所以用起來相當方便。
下面是demo的文件架構,參照yaf手冊建立的,是一個標准的架構:

├── application │ ├── Bootstrap.php │ ├── controllers │ │ ├── Album.php │ │ ├── Application.php │ │ ├── Blogs.php │ │ ├── Device.php │ │ ├── Error.php │ │ ├── Index.php │ │ └── Product.php │ ├── library │ │ ├── Album │ │ │ ├── Filter │ │ │ │ └── Album.php │ │ │ └── Form │ │ │ └── AlbumForm.php │ │ ├── lYaf │ │ │ └── Layout.php │ │ └── Zend │ │ ├── Db(下面的都是文件夾) │ │ ├── Form │ │ ├── InputFilter │ │ ├── ServiceManager │ │ ├── Stdlib │ │ └── Validator │ ── models │ │ └── Device.php │ ├── modules │ │ └── Mod │ │ ├── controllers │ │ │ ├── Ctrl.php │ │ │ └── Index.php │ │ └── views │ │ ├── ctrl │ │ │ └── index.phtml │ │ ├── index │ │ │ └── index.phtml │ │ └── index.phtml │ └── views │ ├── album │ │ ├── add.phtml │ │ └── index.phtml │ ├── blogs │ │ └── index.phtml │ ├── device │ │ ├── index.phtml │ │ └── list.phtml │ ├── error │ │ └── error.phtml │ ├── index │ │ ├── form.phtml │ │ ├── index.phtml │ │ └── list.phtml │ ├── layouts │ │ └── frontend.phtml │ ├── product │ │ └── info.html │ └── report │ └── index.phtml ├── conf │ ├── application.ini ├─ public ├── css ├── font ├── img ├── index.php └── js
接下來看看配置文件

[common] application.directory = APP_PATH "/application" application.dispatcher.catchException = 1 application.dispatcher.throwException = 1 application.view.ext = 'phtml' application.layout.directory=APP_PATH "/application" "/views" "/layouts" ;application.layoutpath = APP_PATH "/application/views" application.document = "layout.phtml" ;app application.baseUri = '' ;not used application.dispatcher.defaultModule = index application.dispatcher.defaultController = index application.dispatcher.defaultAction = index application.modules = Index,Mod [product : common] ;database 數據庫鏈接設置 database.params.driver = "pdo_mysql" database.params.database = "data" database.params.username = "username" database.params.password = "pwd" database.params.hostname = "127.0.0.1" database.params.port = 3306 database.params.charset = "UTF8" database.params.driver_options.1002 = "SET NAMES utf8" routes.user.type = "regex" routes.user.match = "#^/$#" routes.user.route.module = Mod routes.user.route.controller = Ctrl routes.user.route.action = index routes.user.map.1 = name routes.user.map.2 = value
yaf不可缺少的配置項:application.directory = APP_PATH "/application" 建議把
application.dispatcher.catchException = 1 application.dispatcher.throwException = 1
放在[product : common]外面,因為[product : common]里面的是生產環境。注意這一句:
application.modules = Index,Mod
modules要注意順序,而且名字要與modules下面的文件夾名字一致,包括大小寫。在數據庫配置方面,有一個很重要的設置項:
database.params.charset = "UTF8" database.params.driver_options.1002 = "SET NAMES utf8"
如果不設置database.params.driver_options.1002 = "SET NAMES utf8",從數據庫取出來的中文會變成亂碼。
配置文件與Bootstrap.php文件緊密結合,那么再來看Bootstrap.php文件:

<?php use lYaf\Layout; /** * 所有在Bootstrap類中, 以_init開頭的方法, 都會被Yaf調用, * 這些方法, 都接受一個參數:Yaf_Dispatcher $dispatcher * 調用的次序, 和申明的次序相同 */ class Bootstrap extends Yaf\Bootstrap_Abstract { private $_config; public function _initConfig() { $this->_config = Yaf\Application::app()->getConfig(); Yaf\Registry::set("config", $this->_config); } public function _initDefaultName(Yaf\Dispatcher $dispatcher) { $dispatcher->setDefaultModule("Index")->setDefaultController("Index")->setDefaultAction("index"); } /** * 設置頁面layout */ public function _initLayout(Yaf\Dispatcher $dispatcher){ $layout = new Layout($this->_config->application->layout->directory); $dispatcher->setView($layout); } public function _initNamespaces(){ //申明, 凡是以Zend,Local開頭的類, 都是本地類 Yaf\Loader::getInstance()->registerLocalNameSpace(array("Zend", "Local")); } public function _initRoute(Yaf\Dispatcher $dispatcher) { //在這里注冊自己的路由協議,默認使用簡單路由 通過派遣器獲取默認的路由器 $router = Yaf\Dispatcher::getInstance()->getRouter();//獲取路由器 $router->addConfig($this->_config->routes);//加載路由協議 } /** * 連接數據庫,設置數據庫適配器 */ public function _initDefaultDbAdapter(){ $dbAdapter = new Zend\Db\Adapter\Adapter( $this->_config->database->params->toArray() ); Yaf\Registry::set("adapter", $dbAdapter); } }
我已經在這個文件中寫了注釋,在這里就不一一解釋了。因為我開啟了命名空間,所以整個demo都充滿着反斜杠,雖然看起來別扭,但使用起來很方便。在這個Bootstrap.php文件中,使用了一個老外寫的Layout庫,寫得很全面。
從zend 2.2引進來的庫都比較簡單,比1.2的版本小多了,很容易使用。這些引用來的模塊和自定義的模塊(其實引入來的也就是自定義的)都要根據配置,放在相應的目錄下面。在寫這些自定義模塊類時,建議使用yaf的命名空間,提高效率的同時也使代碼簡潔了很多。
默認的module,它的controllers和views就是/application/controllers/和/application/views,而其他module,它們的controllers和views就在它們目錄下面:/modules/Mod/controllers/,/modules/Mod/views/。