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

Symfony 頁面建立完整指南:模組、Action、Template、Routing、Helper、表單與鏈接生成

     編輯:PHP 綜合知識

Symfony 1.x (舊版) 採用 **模組-動作-模板** 架構,雖已停止維護,但其設計思想 (MVC、Routing、Helper、CSRF) 深刻影響後續 Symfony 2+、Laravel 等現代框架。本文系統整理 Symfony 1.x 頁面建立流程:模組初始化、Action/Template 命名慣例、Routing 解耦 URL、Helper 簡化視圖、表單/鏈接生成、請求參數存取,並對應現代 Symfony 寫法對照。

1. 建立模組骨架

# Symfony 1.x 專案根目錄
php symfony init-module frontend article
# 產生目錄結構:
# apps/frontend/modules/article/
#   actions/actions.class.php
#   templates/
#   config/
#   lib/
#   validate/

2. Action 與 Template 命名慣例

  • Action 方法:`execute()` (如 `executeIndex()`, `executeShow()`)
  • Template 檔案:`Success.php` (如 `indexSuccess.php`, `showSuccess.php`)
  • URL 對應:`/frontend_dev.php/article/index` → `article` 模組 `index` action
// apps/frontend/modules/article/actions/actions.class.php
class articleActions extends sfActions
{
    public function executeIndex(sfWebRequest $request)
    {
        // 業務邏輯:從資料庫撈取文章列表
        $this->articles = ArticlePeer::doSelect(new Criteria()); // Propel 1.x
        // 或 Doctrine: $this->articles = Doctrine::getTable('Article')->findAll();

        // 設定頁面標題供 layout 使用
        $this->getResponse()->setTitle('文章列表');
    }

    public function executeShow(sfWebRequest $request)
    {
        $id = $request->getParameter('id');
        $this->article = ArticlePeer::retrieveByPk($id);
        $this->forward404Unless($this->article);
    }

    public function executeNew(sfWebRequest $request)
    {
        $this->form = new ArticleForm();
    }

    public function executeCreate(sfWebRequest $request)
    {
        $this->form = new ArticleForm();
        $this->processForm($request, $this->form);
        $this->setTemplate('new'); // 失敗時顯示 newSuccess.php
    }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()));
        if ($form->isValid()) {
            $article = $form->save();
            $this->redirect('article/show?id=' . $article->getId());
        }
    }
}

3. Template (視圖) 撰寫



文章列表

標題發布時間操作
getTitle(), 'article/show?id='.$article->getId()) ?> getCreatedAt(), 'f') ?> getId()) ?> getId(), 'method=delete confirm=確定刪除?') ?>

4. Routing (路由) 解耦 URL

編輯 `apps/frontend/config/routing.yml`:

article:
  url:   /articles
  param: { module: article, action: index }

article_show:
  url:   /articles/:id
  param: { module: article, action: show }
  requirements: { id: \d+ }

article_new:
  url:   /articles/new
  param: { module: article, action: new }

article_edit:
  url:   /articles/:id/edit
  param: { module: article, action: edit }
  requirements: { id: \d+ }

article_delete:
  url:   /articles/:id
  param: { module: article, action: delete }
  requirements: { id: \d+ }
  method: [delete]

# 首頁
homepage:
  url:   /
  param: { module: article, action: index }

現在 URL 變為美觀的 `/articles/123`,且 Action 程式碼完全不用修改。

5. Helper (視圖助手) 簡化 HTML 生成

Helper用途範例
link_to()生成 連結
url_for()產生絕對/相對 URL
form_tag()開啟
input_tag()getTitle()) ?>
textarea_tag()