This article mainly introduces the Zend_Layout Layout assistant usage in the ZendFramework tutorial, and analyzes the implementation skills of Layout in detail based on the instance form, for more information, see the Zend_Layout layout Assistant in the Zend Framework tutorial. We will share this with you for your reference. The details are as follows:
I. Role
The role of the layout is similar to that of the template. It can be considered that the general and public parts of the website are taken out as the general page framework. For example, for a basic web page, the header and tail of the page may be the same, but the content body may be different. You can make the public part into a template. This not only improves development efficiency, but also facilitates later maintenance.
Ii. Use
Here is a simple example.
First, use zend studio to create a basic zend framework project: layout_demo1
The structure is roughly as follows:
─ ──. Settings
├ ── Application
│ ─ ── Configs
│ ├ ── Controllers
│ ─ ── Models
│ └ ── Views
│ ─ ── Helpers
│ ─ ── Scripts
│ ├ ── Error
│ └ ── Index
├ ── Docs
├-Library
─ ── Public
Deletests
├ ── Application
│ └ ── Controllers
└-Library
1. added the layout function:
Add the following configuration to the application configuration file/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. corresponding directory and layout template files/Layout_demo2/application/layouts/scripts/layout. phtml
├ ── Application
│ ─ ── Configs
│ ├ ── Controllers
│ ─ ── Layouts
│ ─-Scripts
│ ─ ── Models
│ └ ── Views
Layout.html is similar to the following:
my app header
<?php echo $this -> layout() -> content;?>
header
Here
<?php echo $this -> layout() -> content;?>
Is more important. It indicates the layout content, that is, the dynamic change.
In this way, run the program
Www.localzend.com/layout_demo1/public/
The generated html source code is as follows:
my app header
Welcome to the Zend Framework! This is your project's main page
Helpful Links:
Zend Framework Website | Zend Framework Manual
header
The middle part is the content of/layout_demo1/application/views/scripts/index. phtml.
Injection: You can use the zf command tool to automatically generate layout configurations and files.
The command is as follows:
zf enable layout
Refer to the command line section.
Iii. Configuration
1. You can use the application. ini configuration file to configure the storage location and name of the layout file. For example:
resources.layout.layoutPath = APPLICATION_PATH "/mylayouts/scripts"resources.layout.layout = "mylayout"
2. Use the layout object in action
You can use
$layout = $this->_helper->layout();
Or
$helper = $this->_helper->getHelper('Layout');$layout = $helper->getLayoutInstance();
Obtain the layout object.
You can disable the layout mode of the current action as follows:
$layout->disableLayout();
You can use
$layout->setLayout('other');
To use another layout file.
You can pass the value assignment.
$layout->assign('headertitle', 'app title');$layout->somekey = "value"
3. Other methods for obtaining layout objects
(1)
$layout = Zend_Layout::getMvcInstance();
(2)
$layout = $bootstrap->getResource('Layout');
IV. Other usage and implementation principles
For more information, see
Zend_Layout_Controller_Action_Helper_Layout class,
Zend_Layout_Controller_Plugin_Layout class
Zend_View_Helper_Layout class
It is self-evident.
<?php/** 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 License */class 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)); }}
<?php/** 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; } }}
<?php/** Zend_View_Helper_Abstract.php */require_once 'Zend/View/Helper/Abstract.php';/** * View helper for retrieving layout object * * @package Zend_View * @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 License */class 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(); }}