ZF1 採用 MVC 模式,單一入口點 (index.php)、Front Controller 分派、Action Controller 處理請求、View 渲染輸出。本文從 PHP 環境檢查、原始碼下載、目錄建立、核心配置、到第一個 Hello World 完整呈現。
extension=pdo_mysql (或其他資料庫驅動)mod_rewrite 模組載入,且 AllowOverride All 讓 .htaccess 生效date_default_timezone_set('Asia/Taipei')官方已停止維護,建議使用社群維護版 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 (可選)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
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();
[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
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
}
}
escape($this->title); ?>
escape($this->message); ?>
當前時間:
class ErrorController extends Zend_Controller_Action {
public function errorAction() {
$errors = $this->_getParam('error_handler');
$this->view->message = '發生錯誤:' . $errors->exception->getMessage();
}
}
public/。https://localhost/ 應顯示 Hello World 頁面。https://localhost/nonexist 觸發 ErrorController。| 現象 | 原因 | 解決 |
|---|---|---|
| 白畫面/500 | PHP 錯誤未顯示 | 開啟 display_errors、檢查 error_log |
| 404 所有路由 | mod_rewrite 未啟用或 .htaccess 無效 | 檢查 Apache AllowOverride All、LoadModule rewrite_module |
| Class 'Zend_...' not found | include_path 錯誤或 Autoloader 未註冊 | 確認 set_include_path 包含 library、Autoloader::getInstance() |
| View not found | 腳本路徑錯誤 | 檢查 resources.view.scriptPath 或模組結構 |