国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

Zend Framework教程之Zend_Layout布局助手詳解

2020-03-22 18:22:35
字體:
來源:轉載
供稿:網友
本文實例講述了Zend Framework教程之Zend_Layout布局助手。分享給大家供大家參考,具體如下:一、作用布局的作用和模版的作用類似。可以認為是把網站通用、公共的部分拿出來作為通用的頁面框架。例如一個基本的web頁面,可能頁面的頭和尾都是一樣,不一樣的可能只是內容body部分不一樣,可以把公共的部分做成模版。不僅可以提高開發效率,也為后期的維護帶來方便。二、使用這里舉一個簡單的例子。首先用zend studio創建一個基本的zend framework項目:layout_demo1結構大概如下“├─.settings
├─application
│ ├─configs
│ ├─controllers
│ ├─models
│ └─views
│ ├─helpers
│ └─scripts
│ ├─error
│ └─index
├─docs
├─library
├─html' target='_blank'>public
└─tests
├─application
│ └─controllers
└─library1.加入layout功能:應用配置文件/layout_demo2/application/configs/application.ini,加入如下配置resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"resources.frontController.params.displayExceptions = 0resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"[staging : production]2.相應的目錄和布局模版文件 /layout_demo2/application/layouts/scripts/layout.phtml├─application
│ ├─configs
│ ├─controllers
│ ├─layouts
│ │ └─scripts
│ ├─models
│ └─viewslayout.html類似如下: !doctype html html head meta http-equiv="Content-Type" content="text/html;charset=utf-8" title my app /title body div id="header" header /div div id="content" php echo $this - layout() - content; /div div id="footer" header /div /body /html 這里的 php echo $this - layout() - content; 是比較重要的。表示此處為布局的內容,也就是會動態變化的地方。這樣,運行一下程序www.localzend.com/layout_demo1/public/生成的html源碼如下 !doctype html html head meta http-equiv="Content-Type" content="text/html;charset=utf-8" title my app /title body div id="header" header /div div id="content" style a:link, a:visited color: #0398CA; span#zf-name color: #91BE3F; div#welcome color: #FFFFFF; background-image: url(http://framework.zend.com/images/bkg_header.jpg); width: 600px; height: 400px; border: 2px solid #444444; overflow: hidden; text-align: center; div#more-information background-image: url(http://framework.zend.com/images/bkg_body-bottom.gif); height: 100%; /style div id="welcome" h1 Welcome to the span id="zf-name" Zend Framework! /span /h1 h3 This is your project's main page /h3 div id="more-information" p img src="http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png" / /p Helpful Links: br / a Zend Framework Website /a | a Zend Framework Manual /a /div /div /div div id="footer" header /div /body /html 中間部分就是/layout_demo1/application/views/scripts/index/index.phtml的內容。注入:可以通過zf的命令工具自動生成layout的配置和文件。命令如下:zf enable layout可以參考命令行章節三、配置1.自定義存放位置和名稱可以通過application.ini配置文件配置布局文件的存放位置以及布局文件的名稱,例如:resources.layout.layoutPath = APPLICATION_PATH "/mylayouts/scripts"resources.layout.layout = "mylayout"2.在action中使用layout對象可以通過$layout = $this- _helper- layout();或者$helper = $this- _helper- getHelper('Layout');$layout = $helper- getLayoutInstance();獲取布局對象。可以通過如下方式禁用當前action使用布局模式$layout- disableLayout();可以通過$layout- setLayout('other');來設置使用另一個布局文件可以通過來傳遞賦值$layout- assign('headertitle', 'app title');$layout- somekey = "value"3.其它獲取layout對象的方法(1)$layout = Zend_Layout::getMvcInstance();(2)$layout = $bootstrap- getResource('Layout');四、其它用法,實現原理具體其它的使用方法可以參考Zend_Layout_Controller_Action_Helper_Layout類,
Zend_Layout_Controller_Plugin_Layout類
Zend_View_Helper_Layout類
不言自明。/** Zend_Controller_Action_Helper_Abstract */require_once 'Zend/Controller/Action/Helper/Abstract.php'; * Helper for interacting with Zend_Layout objects * @uses Zend_Controller_Action_Helper_Abstract * @category Zend * @package Zend_Controller * @subpackage Zend_Controller_Action * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD Licenseclass Zend_Layout_Controller_Action_Helper_Layout extends Zend_Controller_Action_Helper_Abstract * @var Zend_Controller_Front protected $_frontController; * @var Zend_Layout protected $_layout; * @var bool protected $_isActionControllerSuccessful = false; * Constructor * @param Zend_Layout $layout * @return void public function __construct(Zend_Layout $layout = null) if (null !== $layout) { $this- setLayoutInstance($layout); } else { * @see Zend_Layout require_once 'Zend/Layout.php'; $layout = Zend_Layout::getMvcInstance(); if (null !== $layout) { $pluginClass = $layout- getPluginClass(); $front = $this- getFrontController(); if ($front- hasPlugin($pluginClass)) { $plugin = $front- getPlugin($pluginClass); $plugin- setLayoutActionHelper($this); public function init() $this- _isActionControllerSuccessful = false; * Get front controller instance * @return Zend_Controller_Front public function getFrontController() if (null === $this- _frontController) { * @see Zend_Controller_Front require_once 'Zend/Controller/Front.php'; $this- _frontController = Zend_Controller_Front::getInstance(); return $this- _frontController; * Get layout object * @return Zend_Layout public function getLayoutInstance() if (null === $this- _layout) { * @see Zend_Layout require_once 'Zend/Layout.php'; if (null === ($this- _layout = Zend_Layout::getMvcInstance())) { $this- _layout = new Zend_Layout(); return $this- _layout; * Set layout object * @param Zend_Layout $layout * @return Zend_Layout_Controller_Action_Helper_Layout public function setLayoutInstance(Zend_Layout $layout) $this- _layout = $layout; return $this; * Mark Action Controller (according to this plugin) as Running successfully * @return Zend_Layout_Controller_Action_Helper_Layout public function postDispatch() $this- _isActionControllerSuccessful = true; return $this; * Did the previous action successfully complete * @return bool public function isActionControllerSuccessful() return $this- _isActionControllerSuccessful; * Strategy pattern; call object as method * Returns layout object * @return Zend_Layout public function direct() return $this- getLayoutInstance(); * Proxy method calls to layout object * @param string $method * @param array $args * @return mixed public function __call($method, $args) $layout = $this- getLayoutInstance(); if (method_exists($layout, $method)) { return call_user_func_array(array($layout, $method), $args); require_once 'Zend/Layout/Exception.php'; throw new Zend_Layout_Exception(sprintf("Invalid method '%s' called on layout action helper", $method));
/** Zend_Controller_Plugin_Abstract */require_once 'Zend/Controller/Plugin/Abstract.php'; * Render layouts * @uses Zend_Controller_Plugin_Abstract * @category Zend * @package Zend_Controller * @subpackage Plugins * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @version $Id: Layout.php 23775 2011-03-01 17:25:24Z ralph $class Zend_Layout_Controller_Plugin_Layout extends Zend_Controller_Plugin_Abstract protected $_layoutActionHelper = null; * @var Zend_Layout protected $_layout; * Constructor * @param Zend_Layout $layout * @return void public function __construct(Zend_Layout $layout = null) if (null !== $layout) { $this- setLayout($layout); * Retrieve layout object * @return Zend_Layout public function getLayout() return $this- _layout; * Set layout object * @param Zend_Layout $layout * @return Zend_Layout_Controller_Plugin_Layout public function setLayout(Zend_Layout $layout) $this- _layout = $layout; return $this; * Set layout action helper * @param Zend_Layout_Controller_Action_Helper_Layout $layoutActionHelper * @return Zend_Layout_Controller_Plugin_Layout public function setLayoutActionHelper(Zend_Layout_Controller_Action_Helper_Layout $layoutActionHelper) $this- _layoutActionHelper = $layoutActionHelper; return $this; * Retrieve layout action helper * @return Zend_Layout_Controller_Action_Helper_Layout public function getLayoutActionHelper() return $this- _layoutActionHelper; * postDispatch() plugin hook -- render layout * @param Zend_Controller_Request_Abstract $request * @return void public function postDispatch(Zend_Controller_Request_Abstract $request) $layout = $this- getLayout(); $helper = $this- getLayoutActionHelper(); // Return early if forward detected if (!$request- isDispatched() || $this- getResponse()- isRedirect() || ($layout- getMvcSuccessfulActionOnly() && (!empty($helper) && !$helper- isActionControllerSuccessful()))) return; // Return early if layout has been disabled if (!$layout- isEnabled()) { return; $response = $this- getResponse(); $content = $response- getBody(true); $contentKey = $layout- getContentKey(); if (isset($content['default'])) { $content[$contentKey] = $content['default']; if ('default' != $contentKey) { unset($content['default']); $layout- assign($content); $fullContent = null; $obStartLevel = ob_get_level(); try { $fullContent = $layout- render(); $response- setBody($fullContent); } catch (Exception $e) { while (ob_get_level() $obStartLevel) { $fullContent .= ob_get_clean(); $request- setParam('layoutFullContent', $fullContent); $request- setParam('layoutContent', $layout- content); $response- setBody(null); throw $e;
* @subpackage Helper * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD Licenseclass Zend_View_Helper_Layout extends Zend_View_Helper_Abstract /** @var Zend_Layout */ protected $_layout; * Get layout object * @return Zend_Layout public function getLayout() if (null === $this- _layout) { require_once 'Zend/Layout.php'; $this- _layout = Zend_Layout::getMvcInstance(); if (null === $this- _layout) { // Implicitly creates layout object $this- _layout = new Zend_Layout(); return $this- _layout; * Set layout object * @param Zend_Layout $layout * @return Zend_Layout_Controller_Action_Helper_Layout public function setLayout(Zend_Layout $layout) $this- _layout = $layout; return $this; * Return layout object * Usage: $this- layout()- setLayout('alternate'); * @return Zend_Layout public function layout() return $this- getLayout();更多關于zend相關內容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優秀開發框架總結》、《Yii框架入門及常用技巧總結》、《ThinkPHP入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》希望本文所述對大家PHP程序設計有所幫助。PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 遵义市| 全椒县| 红安县| 新疆| 长武县| 大埔区| 珠海市| 乌兰察布市| 莆田市| 芜湖市| 旌德县| 竹山县| 福清市| 盖州市| 黄大仙区| 南部县| 临夏县| 武鸣县| 濉溪县| 邵阳县| 荥经县| 兴义市| 桃园县| 吉安县| 定远县| 五大连池市| 罗甸县| 鹤峰县| 炉霍县| 福清市| 阿克陶县| 蒲城县| 盖州市| 淮滨县| 营山县| 霍城县| 鸡西市| 辽阳市| 塔河县| 景宁| 益阳市|