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

Zend Framework 入門:環境需求、目錄結構與 Hello World 完整建置步驟

     編輯:PHP 相關知識

Zend Framework 入門:環境需求、目錄結構與 Hello World 完整建置步驟

從零開始建立 Zend Framework 1.x 專案,涵蓋 PHP/Apache 環境檢查、原始碼取得、專案目錄規劃、入口檔與控制器撰寫、視圖渲染的完整流程。

環境前置檢查

  • PHP ≥ 5.2.0(建議 5.3+)
  • php.ini 啟用 extension=php_pdo.dll 及對應 PDO_MYSQL
  • Apache httpd.conf 載入 mod_rewrite 模組
  • AllowOverride All 讓 .htaccess 生效

專案目錄結構

project/
├── application/
│   ├── controllers/     # IndexController.php
│   ├── models/
│   └── views/scripts/   # index.phtml
├── library/
│   └── Zend/            # 框架核心庫
└── public/
    └── index.php        # 入口檔

關鍵程式碼

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();

application/controllers/IndexController.php

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');
    }
}

application/views/scripts/index.phtml

<?=$this->bodyTitle; ?>

常見問題

  • library/Zend 目錄需自行從官網下載框架包解壓放入。
  • 若遇 No adapter found 錯誤,檢查 PDO 擴展是否正確載入。

完成上述步驟,瀏覽 https://localhost/zendframework/ 即可看到「Hello World!」。

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