首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

Zend Framework 入門核心知識點總結:MVC 架構、Front Controller、View、Model 速查

     編輯:PHP 綜合知識

前言:ZF1 核心三件套

Zend Framework 1.x 以 Zend_Controller_Front (前端控制器)、Zend_View (視圖)、Zend_Db_Table (模型) 為核心。本文提煉最關鍵的入門知識點,作為快速上手與面試複習速查表。

一、Front Controller (Zend_Controller_Front)

  • 單例模式:Zend_Controller_Front::getInstance() 取得實例,不可 new。
  • 設定控制器目錄:$front->setControllerDirectory('./application/controllers') 或陣列指定模組。
  • 分派:$front->dispatch() 接管請求,路由 → 分派 → 行動。
  • 常用參數:
    $front->setParam('noViewRenderer', true)   // 關閉 ViewRenderer (用自定義 View)
    $front->setParam('noErrorHandler', true)   // 關閉預設 ErrorController
    $front->setBaseUrl('/subdir')              // 子目錄部署
    $front->throwExceptions(true)              // 開發環境拋出異常
  • 插件系統:$front->registerPlugin(new My_Plugin()) 掛載 routeStartup、dispatchLoopStartup 等鉤子。

二、Action Controller (Zend_Controller_Action)

  • 繼承 Zend_Controller_Action,方法命名 {ActionName}Action (如 indexAction)。
  • init():構造後、分派前執行,適合初始化 View、ACL、認證。
  • preDispatch() / postDispatch():每個 Action 前/後執行。
  • 請求物件:$this->_request (getParam(), getPost(), isPost())。
  • 回應物件:$this->_response (setBody(), setHeader())。
  • 重導向:$this->_redirect('/url')、$this->_forward('action', 'controller', 'module')。
  • 輔助器:$this->_helper->viewRenderer、flashMessenger、layout、json。

三、View (Zend_View)

  • 實例化:$view = new Zend_View(); $view->setScriptPath('./application/views/scripts')。
  • 註冊到 Registry:Zend_Registry::set('view', $view),Controller 取用 Zend_Registry::get('view')。
  • 變量賦值:$view->var = 'value' 或 $view->assign(['a'=>1, 'b'=>2])。
  • 渲染:echo $view->render('index/index.phtml')。
  • 助手:$this->escape(), $this->url(), $this->baseUrl(), $this->headTitle(), $this->headLink()。
  • 關閉預設 ViewRenderer:$front->setParam('noViewRenderer', true) 後需手動渲染。
  • 整合 Smarty:$view = new Zend_View_Smarty(); 並設定 template_dir、compile_dir。

四、Model (Zend_Db_Table)

  • 繼承 Zend_Db_Table_Abstract,指定 $_name (表名)、$_primary (主鍵)。
  • 預設適配器:Zend_Db_Table::setDefaultAdapter($db) (Bootstrap 設定)。
  • CRUD:insert(), update($data, $where), delete($where), find($id), fetchRow($select), fetchAll($select)。
  • Row/Rowset:find() 回傳 Rowset,->current() 取 Row;Row 支援 save(), delete(), toArray()。
  • Zend_Db_Select 鏈式查詢:$table->select()->where()->order()->limitPage()。

五、路由與 URL 結構

  • 預設路由:/:module/:controller/:action/*。
  • URL 範例:/user/edit/id/123 → module=default, controller=user, action=edit, params=[id=123]。
  • 自定義路由:$router->addRoute('name', new Zend_Controller_Router_Route(...))。
  • URL 組裝:$this->view->url(['action'=>'edit', 'id'=>123])。

六、目錄結構最佳實踐

application/
  configs/        # application.ini
  controllers/    # IndexController.php, ErrorController.php
  models/         # Message.php, User.php
  views/
    scripts/      # index/index.phtml, error/error.phtml
    helpers/      # 視圖助手
    filters/      # 視圖過濾器
  layouts/        # layout.phtml (Zend_Layout)
library/
  Zend/           # 框架核心
public/
  index.php       # 入口
  .htaccess       # rewrite
  css/, js/, img/

七、常見錯誤快速定位

錯誤原因解決
Invalid controller specified控制器類名/檔案名不符 (須 PascalCase + Controller.php)檢查類名 IndexController、檔案 IndexController.php
View script not found腳本路徑錯誤確認 setScriptPath、模組結構 views/scripts/controller/action.phtml
No adapter foundZend_Db_Table 未設定預設適配器Bootstrap 中 Zend_Db_Table::setDefaultAdapter($db)
Plugin not found自動載入失敗確認 library 在 include_path、類名符合 PSR-0/命名規則

結語

掌握 **Front Controller 单例分派、Action Controller 生命週期、View 變量賦值與渲染、Zend_Db_Table 表模型 CRUD、路由 URL 對應、目錄結構規範** 六大塊,即可在 ZF1 專案中游刃有餘。理解這些核心機制,遷移至 Laminas (ZF3)、Symfony、Laravel 時,前端控制器→Kernel、Action→Controller、View→Twig/Blade、Table→Entity/Repository,概念完全映射,唯語法與組件化程度不同。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved