欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Zend Framework教程之Zend_Layout布局助手详解

程序员文章站 2024-04-01 19:34:22
本文实例讲述了zend framework教程之zend_layout布局助手。分享给大家供大家参考,具体如下: 一、作用 布局的作用和模版的作用类似。可以认为是把网站...

本文实例讲述了zend framework教程之zend_layout布局助手。分享给大家供大家参考,具体如下:

一、作用

布局的作用和模版的作用类似。可以认为是把网站通用、公共的部分拿出来作为通用的页面框架。例如一个基本的web页面,可能页面的头和尾都是一样,不一样的可能只是内容body部分不一样,可以把公共的部分做成模版。不仅可以提高开发效率,也为后期的维护带来方便。

二、使用

这里举一个简单的例子。

首先用zend studio创建一个基本的zend framework项目:layout_demo1

结构大概如下“

├─.settings
├─application
│  ├─configs
│  ├─controllers
│  ├─models
│  └─views
│      ├─helpers
│      └─scripts
│          ├─error
│          └─index
├─docs
├─library
├─public
└─tests
    ├─application
    │  └─controllers
    └─library

1.加入layout功能:

应用配置文件/layout_demo2/application/configs/application.ini,加入如下配置

resources.frontcontroller.controllerdirectory = application_path "/controllers"
resources.frontcontroller.params.displayexceptions = 0
resources.layout.layoutpath = application_path "/layouts/scripts/"
[staging : production]

2.相应的目录和布局模版文件 /layout_demo2/application/layouts/scripts/layout.phtml

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

layout.html类似如下:

<!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>

这里的

<?php echo $this -> layout() -> content;?>

是比较重要的。表示此处为布局的内容,也就是会动态变化的地方。

这样,运行一下程序

www.localzend.com/layout_demo1/public/

生成的html源码如下

<!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://framework.zend.com/images/poweredby_zf_4lightbg.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>

中间部分就是/layout_demo1/application/views/scripts/index/index.phtml的内容。

注入:可以通过zf的命令工具自动生成layout的配置和文件。

命令如下:

zf enable layout

可以参考命令行章节

三、配置

1.自定义存放位置和名称可以通过application.ini配置文件配置布局文件的存放位置以及布局文件的名称,例如:

resources.layout.layoutpath = application_path "/mylayouts/scripts"
resources.layout.layout = "mylayout"

2.在action中使用layout对象

可以通过

$layout = $this->_helper->layout();

或者

$helper = $this->_helper->gethelper('layout');
$layout = $helper->getlayoutinstance();

获取布局对象。

可以通过如下方式禁用当前action使用布局模式

$layout->disablelayout();

可以通过

$layout->setlayout('other');

来设置使用另一个布局文件

可以通过来传递赋值

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

3.其它获取layout对象的方法

(1)

$layout = zend_layout::getmvcinstance();

(2)

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

四、其它用法,实现原理

具体其它的使用方法可以参考

zend_layout_controller_action_helper_layout类,
zend_layout_controller_plugin_layout类
zend_view_helper_layout类
不言自明。

<?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();
 }
}

更多关于zend相关内容感兴趣的读者可查看本站专题:《zend framework框架入门教程》、《php优秀开发框架总结》、《yii框架入门及常用技巧总结》、《thinkphp入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。