This article describes the Zend_Layout layout assistant in Zend Framework tutorial with examples. Share it with everyone for your reference, the details are as follows:
1. Function
The function of layout is similar to that of template. It can be thought of as taking out the general and public parts of the website as a general page framework. For example, for a basic web page, the head and tail of the page may be the same. The only difference may be the body part of the content. The common part can be made into a template. Not only can it improve development efficiency, but it also brings convenience to later maintenance.
2. Use
Here is a simple example.
First create a basic zend framework project using zend studio: layout_demo1
The structure is roughly as follows”
├─.settings
├─application
│ ├─configs
│ ├─controllers
│ ├─models
│ └─views
│ ├─helpers
│ └─scripts
│ ├─error
│ └─index
├─docs
├─library
├─public
└─tests
├─application
│ └─controllers
└─library
1. Add layout function:
Application configuration file/layout_demo2/application/configs/application.ini, add the following configuration
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" resources.frontController.params.displayExceptions = 0 resources.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:
<!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>
Here
<?php echo $this -> layout() -> content;?>
is more important. Indicates that this is the content of the layout, which is the place that will change dynamically.
In this way, run the program
www.localzend.com/layout_demo1/public/
The generated html source code is as follows
<!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://www.bkjia.com/uploads/allimg/160306/003T934L-0.png" /></p> <p> Helpful Links: <br /> <a href="http://framework.zend.com/">Zend Framework Website</a> | <a href="http://framework.zend.com/manual/en/">Zend Framework Manual</a> </p> </div> </div> </div> <div id="footer"> header </div> </body> </html>
The middle part is the content of /layout_demo1/application/views/scripts/index/index.phtml.
Injection: Layout configuration and files can be automatically generated through zf’s command tool.
The command is as follows:
zf enable layout
You can refer to the command line chapter
3. Configuration
1. Customize the storage location and name. You can configure the storage location and name of the layout file through the application.ini configuration file, for example:
resources.layout.layoutPath = APPLICATION_PATH "/mylayouts/scripts" resources.layout.layout = "mylayout"
2. Use layout object in action
Can be passed
$layout = $this->_helper->layout();
or
$helper = $this->_helper->getHelper('Layout'); $layout = $helper->getLayoutInstance();
Get the layout object.
You can disable the current action using layout mode in the following ways
$layout->disableLayout();
Can be passed
$layout->setLayout('other');
To set up using another layout file
Assignment can be passed through
$layout->assign('headertitle', 'app title'); $layout->somekey = "value"
3. Other methods of obtaining layout objects
(1)
$layout = Zend_Layout::getMvcInstance();
(2)
$layout = $bootstrap->getResource('Layout');
4. Other usages and implementation principles
For other specific usage methods, please refer to
Zend_Layout_Controller_Action_Helper_Layout class,
Zend_Layout_Controller_Plugin_Layout class
Zend_View_Helper_Layout class
Self-explanatory.
<?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(); } }
Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone in PHP programming.