无为清净楼资源网 Design By www.qnjia.com

本文实例讲述了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">
    <"footer">
    header
   </div>
  </body>
</html>

这里的

<"htmlcode">
<!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="/UploadFiles/2021-04-02/PoweredBy_ZF_4LightBG.png">

中间部分就是/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类
不言自明。

<"Invalid method '%s' called on layout action helper", $method));
 }
}

<"htmlcode">
<"_blank" href="https://www.jb51.net/Special/546.htm">Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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

标签:
Zend,Framework,Zend_Layout,布局

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?