從零開始建立 Zend Framework 1.x 專案,涵蓋 PHP/Apache 環境檢查、原始碼取得、專案目錄規劃、入口檔與控制器撰寫、視圖渲染的完整流程。
extension=php_pdo.dll 及對應 PDO_MYSQLhttpd.conf 載入 mod_rewrite 模組AllowOverride All 讓 .htaccess 生效project/
├── application/
│ ├── controllers/ # IndexController.php
│ ├── models/
│ └── views/scripts/ # index.phtml
├── library/
│ └── Zend/ # 框架核心庫
└── public/
└── index.php # 入口檔
set_include_path('.' . PATH_SEPARATOR . './library' . PATH_SEPARATOR . './application/models/' . PATH_SEPARATOR . get_include_path());
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
$view = new Zend_View();
$view->setScriptPath('./application/views/scripts/');
Zend_Registry::set('view', $view);
$front = Zend_Controller_Front::getInstance();
$front->setBaseUrl('/zendframework')
->setParam('noViewRenderer', true)
->setControllerDirectory('./application/controllers')
->throwExceptions(true)
->dispatch();
class IndexController extends Zend_Controller_Action {
public function init() {
$this->view = Zend_Registry::get('view');
$this->view->baseUrl = $this->_request->getBaseUrl();
}
public function indexAction() {
$this->view->bodyTitle = '<h1>Hello World!</h1>';
echo $this->view->render('index.phtml');
}
}
<?=$this->bodyTitle; ?>
library/Zend 目錄需自行從官網下載框架包解壓放入。No adapter found 錯誤,檢查 PDO 擴展是否正確載入。完成上述步驟,瀏覽 https://localhost/zendframework/ 即可看到「Hello World!」。