


Zend Framework tutorial Zend_Layout layout assistant detailed explanation, zendzend_layout_PHP tutorial
Detailed explanation of Zend_Layout layout assistant in Zend Framework tutorial, zendzend_layout
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.
Articles you may be interested in:
- Zend Framework Tutorial: Basic Model Rules and Usage Methods
- How to use memcache in zend framework
- zend Solution to the url case problem in the framework
- Zend Framework 2.0 Event Manager (The EventManager) introductory tutorial
- Zend Framework page caching example
- Very easy to use Zend Framework Pagination class
- Detailed explanation of Layout (modular layout) in zend Framework
- zend framework configuration operation database instance analysis
- Zendframework project environment construction under windows (configuration through command line )
- Zend Framework Tutorial: Simple Example of Model Usage

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In Windows 11, the Start menu has been redesigned and features a simplified set of apps arranged in a grid of pages, unlike its predecessor, which had folders, apps, and apps on the Start menu. Group. You can customize the Start menu layout and import and export it to other Windows devices to personalize it to your liking. In this guide, we’ll discuss step-by-step instructions for importing Start Layout to customize the default layout on Windows 11. What is Import-StartLayout in Windows 11? Import Start Layout is a cmdlet used in Windows 10 and earlier versions to import customizations for the Start menu into
![SCNotification has stopped working [5 steps to fix it]](https://img.php.cn/upload/article/000/887/227/168433050522031.png?x-oss-process=image/resize,m_fill,h_207,w_330)
As a Windows user, you are likely to encounter SCNotification has stopped working error every time you start your computer. SCNotification.exe is a Microsoft system notification file that crashes every time you start your PC due to permission errors and network failures. This error is also known by its problematic event name. So you might not see this as SCNotification having stopped working, but as bug clr20r3. In this article, we will explore all the steps you need to take to fix SCNotification has stopped working so that it doesn’t bother you again. What is SCNotification.e

Windows 11 brings a lot to the table in terms of user experience, but the iteration isn't entirely error-proof. Users run into issues from time to time, and changes to icon positioning are common. So how to save desktop layout in Windows 11? There are built-in and third-party solutions for this task, whether it's saving the screen resolution of the current window or the arrangement of desktop icons. This becomes even more important for users who have a bunch of icons on their desktop. Read on to learn how to save desktop icon locations in Windows 11. Why doesn't Windows 11 save icon layout positions? Here are the main reasons why Windows 11 does not save desktop icon layout: Changes to display settings: Typically, when you modify display settings, the configured customizations

Guide to solving misaligned WordPress web pages In WordPress website development, sometimes we encounter web page elements that are misaligned. This may be due to screen sizes on different devices, browser compatibility, or improper CSS style settings. To solve this misalignment, we need to carefully analyze the problem, find possible causes, and debug and repair it step by step. This article will share some common WordPress web page misalignment problems and corresponding solutions, and provide specific code examples to help develop

How to create a responsive carousel layout using HTML and CSS Carousels are a common element in modern web design. It can attract the user's attention, display multiple contents or images, and switch automatically. In this article, we will introduce how to create a responsive carousel layout using HTML and CSS. First, we need to create a basic HTML structure and add the required CSS styles. The following is a simple HTML structure: <!DOCTYPEhtml&g

In early trading on December 1, 2023, the three major stock indexes opened lower. The Robot ETF (562500) began to trade sideways after falling early in the session. As of 10:20, the Robot ETF (562500) fell 0.92%, with more than 60 of the 82 holdings falling. Daheng Technology and Shitou Technology fell by more than 5%, and Sukron Technology, Keda Intelligence, Xianhui Technology, and Hongxun Technology fell by more than 3%. As of early trading today, the Robot ETF (562500) has been correcting for three consecutive days. Looking back on the situation in the past month, the Robot ETF (562500) has only had one correction for three consecutive days, and then ushered in eight consecutive positive trends. This pullback may be a good layout opportunity following the announcement by relevant departments in early November.

How to flexibly use the position attribute in H5. In H5 development, the positioning and layout of elements are often involved. At this time, the CSS position property will come into play. The position attribute can control the positioning of elements on the page, including relative positioning, absolute positioning, fixed positioning and sticky positioning. This article will introduce in detail how to flexibly use the position attribute in H5 development.

When we open multiple windows at the same time, win7 has the function of arranging multiple windows in different ways and then displaying them at the same time, which allows us to view the contents of each window more clearly. So how many window arrangements are there in win7? What do they look like? Let’s take a look with the editor. There are several ways to arrange Windows 7 windows: three, namely cascading windows, stacked display windows and side-by-side display windows. When we open multiple windows, we can right-click on an empty space on the taskbar. You can see three window arrangements. 1. Cascading windows: 2. Stacked display windows: 3. Display windows side by side:
