Zend Framework Tutorial zend_layout Layout assistant, zendzend_layout_php tutorial

Source: Internet
Author: User
Tags zend framework

Zend Framework Tutorial Zend_layout Layout Assistant detailed, zendzend_layout


This example describes the Zend_layout layout assistant of the Zend Framework tutorial. Share to everyone for your reference, as follows:

First, the role

The role of the layout is similar to that of a template. It can be thought that the common and common part of the website is taken out as a common page framework. For example, a basic Web page, may be the head and tail of the page is the same, different may be just the body part of the content 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 to bring convenience.

Second, use

Here is a simple example.

Start with Zend Studio to create a basic Zend Framework project: 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:

Application configuration file/layout_demo2/application/configs/application.ini, add the following configuration

Resources.frontController.controllerDirectory = Application_path "/controllers" Resources.frontController.params.displayExceptions = 0resources.layout.layoutpath = Application_path "/layouts/ Scripts/"[Staging:production]

2. Corresponding catalog and layout template files /layout_demo2/application/layouts/scripts/layout.phtml

├─application
│├─configs
│├─controllers
│├─layouts
││└─scripts
│├─models
│└─views

Layout.html similar to the following:

   
 
    My app         Header          <?php echo $this, layout (), content;? >          Header     

Here's

<?php Echo $this, layout (), content;? >

is more important. Represents the content of the layout here, which is where it changes dynamically.

So, run the program

www.localzend.com/layout_demo1/public/

The generated HTML source code is as follows

   
 
    My app         Header          

Zend framework!

This is your project ' s main Page


Zend Framework Website | Zend Framework Manual

Header

The middle part is the content of/layout_demo1/application/views/scripts/index/index.phtml.

Injection: The configuration and file of layout can be generated automatically through the ZF Command tool.

The command is as follows:

ZF Enable layout

You can refer to the Command line section

Third, the configuration

1. Custom storage location and name you can configure the location of the layout file and the 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. Using the Layout object in action

can be done by

$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 way

$layout->disablelayout ();

can be done by

$layout->setlayout (' other ');

To set the use of another layout file

You can pass assignments by

$layout->assign (' Headertitle ', ' app title '); $layout->somekey = "value"

3. Other ways to get the layout object

(1)

$layout = Zend_layout::getmvcinstance ();

(2)

$layout = $bootstrap->getresource (' layout ');

Iv. other usages, principle of realization

Specific 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 Zend * @ Package Zend_controller * @subpackage zend_controller_action * @copyright Copyright (c) 2005-2011 Zend Technologies USA in C. (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->_la   yout) {/** * @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 * * Pub  Lic function setlayoutinstance (zend_layout $layout) {$this->_layout = $layout; return $this; }/** * Mark Action Controller (according to this plugin) as Running successfully * * @return ZEND_LAYOUT_CONTROLLER_AC  Tion_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-& Gt;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 * @co Pyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/lic ENSE/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 Getla Youtactionhelper () {return $this->_layoutactionhelper;}/** * Postdispatch () plugin hook--Render layout * * @pa Ram zend_controller_request_abstract $request * @return void */Public function Postdispatch (zend_controller_request_ab  Stract $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 Technologie S USA Inc. (http://www.zend.com) * @license HTTP://FRAMEWORK.ZEND.COM/LICENSE/NEW-BSD new BSD license */class Zend_view_h  Elper_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 * * Pub Lic 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 topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Zend Framework Tutorial Model basic rules and how to use them
    • Methods of using Memcache in the Zend Framework
    • Resolution of URL case problem in Zend Framework frame
    • Zend Framework 2.0 Event Manager (the EventManager) Getting Started tutorial
    • Zend Framework Page Cache instance
    • Very useful Zend Framework paging class
    • Layout in the Zend Framework (modular layout) detailed
    • Zend Framework Configuration Operations Database instance analysis
    • Windows Zendframework Project Environment Setup (via command line configuration)
    • Zend Framework Tutorial Model Usage Simple Example

http://www.bkjia.com/PHPjc/1106114.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106114.html techarticle Zend Framework Tutorial zend_layout Layout Assistant, Zendzend_layout This example describes the Zend framework tutorial zend_layout Layout Assistant. Share to everyone for your reference, specific as ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.