萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

Zend Framework 1 Layout 佈局機制完整配置指南

Zend Framework 1 的 `Zend_Layout` 元件實現「兩步驟視圖」模式:全域佈局(Layout)包裝各 Action 視圖。正確配置能實現頁面結構復用、區塊化管理與維護性提升。本文詳細說明從配置啟用到佈局腳本撰寫的完整流程。

配置檔啟用 Layout

檔案:application/configs/application.ini

[production]
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.layout.layout = "layout"  ; 預設佈局腳本名稱(layout.phtml)

; 啟用 View 資源(Layout 依賴 View)
resources.view[] =                  ; 空值即可啟用 View
resources.view.doctype = "XHTML1_STRICT"

Bootstrap 初始化

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT'); // 設定文件型態
    }

    // 可選:自訂 Layout 實例化邏輯
    protected function _initLayout()
    {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $layout->setViewSuffix('phtml'); // 視圖副檔名
    }
}

佈局腳本:application/layouts/scripts/layout.phtml

<?php echo $this->doctype() ?>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>ZF Application</title>
    <?php echo $this->headLink()->appendStylesheet('/css/global.css') ?>
    <?php echo $this->headScript()->appendFile('/js/app.js') ?>
</head>
<body>
    <div id="header">
        <div class="logo"><b>ZF Quickstart</b></div>
        <nav>
            <a href="<?php echo $this->url(['controller' => 'guestbook'], 'default', true) ?>">留言板</a>
        </nav>
    </div>

    <div id="content">
        <?php echo $this->layout()->content ?>
    </div>

    <div id="footer">
        © 2024 My App
    </div>
</body>
</html>

關鍵佈局變數與助手

語法用途
$this->layout()->content輸出 Action 視圖內容(核心)
$this->layout()->varName存取佈局變數(控制器可 `$this->_helper->layout()->varName = $val` 設定)
$this->placeholder('key')佔位符,視圖可 `$this->placeholder('key')->set('content')` 注入
$this->headTitle()管理 ``,Action 可 `$this->view->headTitle('頁面標題')->setSeparator(' - ')`</td></tr> <tr><td><code>$this->headLink()</code></td><td>管理 `<link rel="stylesheet">`</td></tr> <tr><td><code>$this->headScript()</code></td><td>管理 `<script src="">`</td></tr> <tr><td><code>$this->inlineScript()</code></td><td>管理內嵌 `<script>` 區塊</td></tr> </table> <h2>控制器中切換/停用佈局</h2> <pre><code><?php class ApiController extends Zend_Controller_Action { public function init() { // 1. 停用佈局(純 JSON/API 回應) $this->_helper->layout()->disableLayout(); // 2. 或切換其他佈局腳本 // $this->_helper->layout()->setLayout('ajax'); // 3. 禁用 View 渲染(純輸出) // $this->_helper->viewRenderer->setNoRender(true); } public function dataAction() { $this->_helper->json(['status' => 'ok', 'time' => time()]); } }</code></pre> <h2>模組專屬佈局</h2> <p>模組目錄下建立 `layouts/scripts/layout.phtml`,並在模組 `Bootstrap.php` 設定:</p> <pre><code>class Blog_Bootstrap extends Zend_Application_Module_Bootstrap { protected function _initLayout() { $this->bootstrap('layout'); $layout = $this->getResource('layout'); $layout->setLayoutPath(APPLICATION_PATH . '/modules/blog/layouts/scripts'); } }</code></pre> <h2>常見問題</h2> <ul> <li><strong>雙重輸出</strong>:若 Action 視圖已包含完整 HTML 結構,佈局又包一層 → 產生巢狀 `<html><body>`。解決:Action 視圖只輸出片段,或停用佈局。</li> <li><strong>CSS/JS 路徑錯誤</strong>:使用 `$this->baseUrl('/css/style.css')` 產生正確相對路徑。</li> <li><strong>編碼問題</strong>:確認 `resources.view.encoding = "UTF-8"` 且資料庫連線 `SET NAMES utf8`。</li> </ul> <p>Layout 機制讓 ZF1 專案擁有清晰的「頁面骨架與內容分離」架構,善用 View Helper(headTitle, headLink, placeholder)可進一步提升視圖組裝靈活度。</p> <div class="ad2"> <script language='javascript' src='/ad/tad.js'></script> </div> </div> <div> <div class="pageNum"> <ul> <li>上一篇程序編程: <a class='LinkPrevArticle' href='https://twcomputer.wsxdn.com/programming/networkprogramming/php/201510/66374.html' >PHP 遞迴遍歷目錄取得所有檔案與子目錄完整實作</a> <li>下一篇程序編程: <a class='LinkNextArticle' href='https://twcomputer.wsxdn.com/programming/networkprogramming/php/201510/66376.html' >Delphi 實作程式閒置 30 秒無鍵盤滑鼠動作自動關閉</a> </ul> </div> </div> <div class=kenbxne_js></div> </div> <div class=kenbxne_js></div> </div> <div class=kenbxne_js></div> <div class=hebxbne_left-div-2> <h2 style="text-align:left">PHP 程式設計排行</h2> <ul> <li><a class="font7 nleft" title="PHP 5 三大核心特性深度解析:新對象模型、異常處理、命名空間" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201705/176557.html">PHP 5 三大核心特性深度解析:新對象模型、異常處理、命名空間</a></li> <li><a class="font7 nleft" title="PHP 類別屬性可見性實戰:public、private、protected 權…" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201705/177313.html">PHP 類別屬性可見性實戰:public、private、protected 權…</a></li> <li><a class="font7 nleft" title="PHP 異常處理機制全面指南:從基礎到進階實戰" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201510/64364.html">PHP 異常處理機制全面指南:從基礎到進階實戰</a></li> <li><a class="font7 nleft" title="PHP 檔案下載實現:Header + readfile 方案的優缺點與生產級替…" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201510/65863.html">PHP 檔案下載實現:Header + readfile 方案的優缺點與生產級替…</a></li> <li><a class="font7 nleft" title="PHP Session 併發阻塞問題與 session_write_close(…" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201510/65144.html">PHP Session 併發阻塞問題與 session_write_close(…</a></li> </ul> </div> <div class=kenbxne_js></div> </div> <div class="nright hebxbne_right"> <div class=guanggao1 id=guanggao_r1></div> <div class=kenbxne_js></div> <div class=hebxbne_right-div1> <h2>推薦文章</h2> <div class=kenbxne_js></div> <ul class=hebxbne_right-list1> <li><a class="font7 nleft" title="PHP 读写制表符(TSV)分隔文件:write_tabbed_file 与 l…" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201510/64240.html">PHP 读写制表符(TSV)分隔文件:write_tabbed_file 与 l…</a></li> <li><a class="font7 nleft" title="PHP 動態生成 IP 簽名檔圖片:GD 庫繪製、代理 IP 偵測、透明背景優化" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201705/176358.html">PHP 動態生成 IP 簽名檔圖片:GD 庫繪製、代理 IP 偵測、透明背景優化</a></li> <li><a class="font7 nleft" title="PHP 字串壓縮演算法效能大比拚:gzcompress、gzdeflate、gz…" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201510/65890.html">PHP 字串壓縮演算法效能大比拚:gzcompress、gzdeflate、gz…</a></li> <li><a class="font7 nleft" title="PHP 運算子完整指南:算術、指派、比較、邏輯與優先序" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201705/177268.html">PHP 運算子完整指南:算術、指派、比較、邏輯與優先序</a></li> <li><a class="font7 nleft" title="PHP 陣列合併與追加完全指南:array_merge、array_merge_…" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201705/177636.html">PHP 陣列合併與追加完全指南:array_merge、array_merge_…</a></li> <li><a class="font7 nleft" title="PHP 產生自訂長度隨機字串函數" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201510/65427.html">PHP 產生自訂長度隨機字串函數</a></li> <li><a class="font7 nleft" title="PHP 靜態頁面生成策略總結:三大模式與兩種部署架構" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201705/176379.html">PHP 靜態頁面生成策略總結:三大模式與兩種部署架構</a></li> <li><a class="font7 nleft" title="PHP 中 die() 與 exit() 的真正差異:其實它們是同一個函式" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201705/177653.html">PHP 中 die() 與 exit() 的真正差異:其實它們是同一個函式</a></li> <li><a class="font7 nleft" title="PHP XSS 防護深度指南:從內建函式到完整過濾器實作" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201510/65385.html">PHP XSS 防護深度指南:從內建函式到完整過濾器實作</a></li> <li><a class="font7 nleft" title="PHP similar_text() 计算字符串相似度:原理、百分比返回、拼写纠…" href="https://twcomputer.wsxdn.com/programming/networkprogramming/php/201510/64219.html">PHP similar_text() 计算字符串相似度:原理、百分比返回、拼写纠…</a></li> </ul> </div> </div> <div class=kenbxne_js></div> </div> <div class=kenbxne_js></div> <div class=plnebxmj> <div class="plnebxmj-2 flink"> <h2><a class=bottom href="https://twcomputer.wsxdn.com/" target=_blank>萬盛學電腦網</a>| <a class=bottom hreflang="ja" href="https://jp.wsxdn.com/" target=_blank>日本語版</a> | <a class=bottom onclick="this.style.behavior='url(#default#homepage)';this.sethomepage('https://twcomputer.wsxdn.com');" href="#">設為首頁</a> | <a class=bottom href="javascript:window.external.addfavorite('https://twcomputer.wsxdn.com','萬盛學電腦網');">加入收藏</a></h2> </div> <div class=knemnnx></div> </div> <div class="ojwbxnks">copyright © <a title="萬盛學電腦網" href="https://twcomputer.wsxdn.com/" target=_blank>萬盛學電腦網</a> all rights reserved <br> </div> </div> </body> </html>