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

Zend Framework 經典入門教程:從安裝到打造新聞管理系統

     編輯:PHP 相關知識

Zend Framework 經典入門教程:從安裝到打造新聞管理系統

本教程引導你從零開始建立 Zend Framework 專案,涵蓋環境需求、目錄結構、前端控制器、路由重寫、View 渲染、輸入過濾,最終完成一個具備新增、列表、後台審核、評論功能的新聞管理系統。

環境需求與安裝

  • PHP 5.2+、Apache + mod_rewrite
  • 下載 Zend Framework 套件,解壓至專案 lib/ 目錄(或 library/)。
  • 設定 include_path 指向 lib/。

目錄結構規劃

app/
  views/      # 模板檔
  controllers/ # 控制器
www/          # DocumentRoot
  .htaccess   # mod_rewrite 規則
  index.php   # 入口腳本
lib/          # Zend Framework 類庫

.htaccess 重寫規則

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
php_value include_path "/path/to/lib"

入口腳本 index.php

<?php
include 'Zend.php';
Zend::loadClass('Zend_Controller_Front');
$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory('/path/to/controllers');
$controller->dispatch();
?>

控制器基礎:IndexController 與 FooController

<?php
// IndexController.php
Zend::loadClass('Zend_Controller_Action');
class IndexController extends Zend_Controller_Action {
    public function indexAction() { echo 'IndexController::indexAction()'; }
    public function noRouteAction() { $this->_redirect('/'); }
}

// FooController.php
class FooController extends Zend_Controller_Action {
    public function indexAction() { echo 'FooController::indexAction()'; }
    public function barAction() { echo 'FooController::barAction()'; }
    public function __call($action, $args) { echo 'FooController::__call()'; }
}
?>

URL 對應規則:/module/controller/action,預設皆為 index。

View 整合:Zend_View 基礎用法

<?php
// 控制器中
$view = new Zend_View();
$view->setScriptPath('/path/to/views');
$view->title = '動態標題';
$view->body = '動態內容';
echo $view->render('example.php');

模板 example.php:

<html><head><title><?php echo $this->escape($this->title); ?></title></head>
<body><?php echo $this->escape($this->body); ?></body></html>

善用 Zend::register('view', $view) 於 Bootstrap 註冊,Controller 中透過 Zend::registry('view') 取得,避免重複實例化。

輸入過濾:Zend_InputFilter

<?php
$filter = new Zend_InputFilter($_POST);
$name = $filter->getAlpha('name');      // 只保留字母
$email = $filter->getEmail('email');    // 驗證 Email 格式
$id = $filter->getDigits('id');         // 只保留數字
?>

實戰專案:新聞管理系統

URL 規劃

  • / - 新聞列表 (IndexController)
  • /add/news、/add/comment - 新增新聞/評論 (AddController)
  • /admin、/admin/approve - 後台審核 (AdminController)
  • /view/{id} - 單篇新聞與評論 (ViewController)

資料庫操作類別 (SQLite 範例)

<?php
class Database {
    private $_db;
    public function __construct($file) { $this->_db = new SQLiteDatabase($file); }
    public function addNews($title, $content) { /* INSERT ... */ }
    public function addComment($name, $comment, $newsId) { /* INSERT ... */ }
    public function approveNews($ids) { /* UPDATE approval = 'T' ... */ }
    public function getNews($id = 'ALL') { /* SELECT ... */ }
    public function getComments($newsId) { /* SELECT ... */ }
}
?>

Bootstrap 整合

<?php
include 'Zend.php';
function __autoload($class) { Zend::loadClass($class); }
$db = new Database('/path/to/db.sqlite');
Zend::register('db', $db);
$view = new Zend_View();
$view->setScriptPath('/path/to/views');
Zend::register('view', $view);
Zend_Controller_Front::getInstance()
    ->setControllerDirectory('/path/to/controllers')
    ->dispatch();
?>

Controller 實作精簡版

<?php
// IndexController::indexAction
$db = Zend::registry('db');
$view = Zend::registry('view');
$view->news = $db->getNews();
echo $view->render('index.php');

// AddController::newsAction
$filter = new Zend_InputFilter($_POST);
$db = Zend::registry('db');
$db->addNews($filter->noTags('title'), $filter->noTags('content'));
$this->_redirect('/');

// ViewController::__call($id)
$db = Zend::registry('db');
$view = Zend::registry('view');
$view->news = $db->getNews($id);
$view->comments = $db->getComments($id);
$view->id = $id;
echo $view->render('view.php');
?>

延伸資源

結語

Zend Framework 雖早期版本仍在演進,但其 MVC 架構、元件化設計與彈性擴充性已展現強大潛力。掌握本教程核心概念,即可快速開發結構清晰、可維護的 PHP 應用。

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