This example describes the Zend_layout layout Assistant for the Zend Framework tutorial. Share to everyone for your reference, specific as follows:
First, the role
The role of the layout and the role of the template are similar. It can be thought that the common, public part of the site as a common page framework. For example, a basic Web page, may be the head and tail of the page are the same, not the same as the content of the body is not the same, you can make the public part of the template. Not only can improve the development efficiency, but also for the later maintenance bring convenience.
Second, the use
Here's a simple example.
First, create a basic Zend Framework project with Zend Studio: Layout_demo1
The structure is probably as follows
├─.settings
├─application
│├─configs
│├─controllers
│├─models
│└─views
│├─helpers
│└─scripts
│├─error
│└─index
├─docs
├─library
├─public
└─tests
├─application
│└─controllers
└─library
1. Add Layout function:
Apply 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. The corresponding directory and layout template file /layout_demo2/application/layouts/scripts/layout.phtml
├─application
│├─configs
│├─controllers
│├─layouts
││└─scripts
│├─models
│└─views
Layout.html similar to the following:
Here's
<?php echo $this-> layout ()-> content;? >
is more important. Represents the content of the layout here, which is where it will change dynamically.
So, run the program
www.localzend.com/layout_demo1/public/
The generated HTML source code is as follows
The middle part is the content of the/layout_demo1/application/views/scripts/index/index.phtml.
Injection: Layout configuration and files can be generated automatically through the ZF Command tool.
The order is as follows:
You can refer to the command line chapter
Third, the configuration
1. Custom location and name can be configured with the Application.ini profile location of the layout file and the 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
Can pass
$layout = $this->_helper->layout ();
Or
$helper = $this->_helper->gethelper (' Layout ');
$layout = $helper->getlayoutinstance ();
Gets the layout object.
You can disable the current action using layout mode in the following ways
$layout->disablelayout ();
Can pass
$layout->setlayout (' other ');
To set the use of another layout file
You can pass an assignment by
$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 ');
Iv. other uses, principles of implementation
Other methods of use can refer to
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 Ze nd * @package zend_controller * @subpackage zend_controller_action * @copyright Copyright (c) 2005-2011 Zend Technologi Es USA Inc. (http://www.zend.com) * @license HTTP://FRAMEWORK.ZEND.COM/LICENSE/NEW-BSD New BSD License * * Class Zend_la Yout_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 $lay
out = 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/contr
Oller/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_controlle
R_action_helper_layout */Public Function Postdispatch () {$this->_isactioncontrollersuccessful = true;
return $this;
}/** * Did the previous action successfully complete? * * @return bool/Public Function isactioncontrollersuccessful () {return $this->_ISACTIONCONTROLLERSUCCESSFU
L /** * 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 * @subpa Ckage Plugins * @copyright Copyright (c) 2005-2011 Zend Technologies USA INC (http://www.zend.com) * @license http://fr AMEWORK.ZEND.COM/LICENSE/NEW-BSD New BSD License * @version $Id: layout.php 23775 2011-03-01 17:25:24z $ */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 $lay
out = 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_lay
Out $layout) {$this->_layout = $layout;
return $this; /** * Set Layout Action Helper * * @param zend_layout_controller_action_helper_layout $layoutActionHelper * @re Turn 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 * @retur
n 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);
The 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
T /** * Get Layout Object * * @return zend_layout/Public Function getlayout () {if (null = = $this->_layou
T) {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 ();
}
}
More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design.