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

Zend Framework 入門實戰:環境配置、目錄結構、Hello World 完整流程

     編輯:PHP 綜合知識

前言:Zend Framework 1.x 核心架構

ZF1 採用 MVC 模式,單一入口點 (index.php)、Front Controller 分派、Action Controller 處理請求、View 渲染輸出。本文從 PHP 環境檢查、原始碼下載、目錄建立、核心配置、到第一個 Hello World 完整呈現。

一、PHP 環境需求

  • PHP ≥ 5.2.0 (建議 5.6+ 或 7.x 配合 ZF1 維護版)
  • 啟用 PDO 擴展:extension=pdo_mysql (或其他資料庫驅動)
  • Apache mod_rewrite 模組載入,且 AllowOverride All 讓 .htaccess 生效
  • 時區設定:date_default_timezone_set('Asia/Taipei')

二、取得 ZF1 程式碼

官方已停止維護,建議使用社群維護版 zendframework/zf1 (Packagist) 或 GitHub zendframework/zf1。
composer require zendframework/zf1 會安裝至 vendor/zendframework/zf1/library。

三、標準目錄結構

project-root/
├── public/                 <-- DocumentRoot
│   ├── index.php           <-- 單一入口
│   ├── .htaccess           <-- rewrite 規則
│   ├── css/, js/, images/
├── application/
│   ├── configs/
│   │   └── application.ini
│   ├── controllers/
│   │   ├── IndexController.php
│   │   └── ErrorController.php
│   ├── models/
│   └── views/
│       ├── scripts/
│       │   ├── index/
│       │   │   └── index.phtml
│       │   └── error/
│       │       └── error.phtml
│       ├── helpers/
│       └── filters/
├── library/
│   └── Zend/               <-- ZF1 library (或 vendor/.../library)
└── Bootstrap.php (可選)

四、核心配置文件

1. public/.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

2. public/index.php

setFallbackAutoloader(true);

// 載入設定
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'production');
Zend_Registry::set('config', $config);

// 啟動 MVC
$front = Zend_Controller_Front::getInstance();
$front->setControllerDirectory(APPLICATION_PATH . '/controllers')
      ->setParam('noViewRenderer', false) // 使用 ViewRenderer
      ->throwExceptions(true)
      ->dispatch();

3. application/configs/application.ini

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.view.encoding = "UTF-8"
resources.view.doctype = "XHTML1_STRICT"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

五、第一個 Controller & View

application/controllers/IndexController.php

class IndexController extends Zend_Controller_Action {
    public function indexAction() {
        $this->view->title = 'Hello World';
        $this->view->message = '歡迎來到 Zend Framework 1.x!';
        // 預設渲染 views/scripts/index/index.phtml
    }
}

application/views/scripts/index/index.phtml

escape($this->title); ?>

escape($this->message); ?>

當前時間:

六、錯誤處理 Controller

class ErrorController extends Zend_Controller_Action {
    public function errorAction() {
        $errors = $this->_getParam('error_handler');
        $this->view->message = '發生錯誤:' . $errors->exception->getMessage();
    }
}

七、驗證運行

  1. 將 Web Server DocumentRoot 指向 public/。
  2. 瀏覽 https://localhost/ 應顯示 Hello World 頁面。
  3. 嘗試 https://localhost/nonexist 觸發 ErrorController。

八、常見問題快速排查

結語

完成上述步驟,即擁有一個可運行的 ZF1 專案骨架。後續開發只需:controllers/ 新增 Action、models/ 繼承 Zend_Db_Table、views/scripts/ 對應模板。雖 ZF1 官方已 EOL,但其 MVC 思想、元件設計 (Zend_Db, Zend_Form, Zend_Acl, Zend_Cache...) 仍具教學與維舊專案價值。新專案建議直接採用 Laminas (ZF3)、Symfony 或 Laravel。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
現象原因解決
白畫面/500PHP 錯誤未顯示開啟 display_errors、檢查 error_log
404 所有路由mod_rewrite 未啟用或 .htaccess 無效檢查 Apache AllowOverride All、LoadModule rewrite_module
Class 'Zend_...' not foundinclude_path 錯誤或 Autoloader 未註冊確認 set_include_path 包含 library、Autoloader::getInstance()
View not found腳本路徑錯誤檢查 resources.view.scriptPath 或模組結構