 
- Zend 框架教程
- Zend 框架 - 主页
- Zend 框架 - 简介
- Zend 框架 - 安装
- 骨架应用
- Zend 框架 - MVC 架构
- Zend 框架 - 概念
- Zend 框架 - 服务管理器
- Zend 框架 - 事件管理器
- Zend 框架 - 模块系统
- 应用结构
- Zend 框架 - 创建模块
- Zend 框架 - 控制器
- Zend 框架 - 路由
- Zend 框架 - 视图层
- Zend 框架 - 布局
- 模型和数据库
- 不同的数据库
- 表格和验证
- Zend 框架 - 文件上传
- Zend 框架 - Ajax
- Cookie 管理
- 会话管理
- Zend 框架 - 身份验证
- 电子邮件管理
- Zend 框架 - 单元测试
- Zend 框架 - 错误处理
- Zend 框架 - 工作示例
- Zend 框架有用的资源
- Zend 框架 - 快速指南
- Zend 框架 - 有用的资源
- Zend 框架 - 讨论
Zend 框架 - 视图层
视图层是 MVC 应用程序的表示层。它将应用程序逻辑与表示逻辑分开。在典型的 PHP Web 应用程序中,所有业务逻辑和设计都是混合在一起的。混合可以在小项目中实现更快的开发。但是,在涉及大量高级架构的大型项目中,它会惨遭失败。要更改 Web 应用程序的设计,开发人员还需要处理业务逻辑。这可能是灾难性的,会导致业务逻辑的破坏。
Zend Framework 提供了一个经过深思熟虑、干净、灵活且可扩展的视图层。View 层可作为单独的模块Zend/View并与Zend/Mvc模块完美集成。Zend View Layer 被分成多个组件,彼此之间可以很好地交互。
其各个组成部分如下 -
- 变量容器- 保存视图层的数据。 
- 查看模型- 保存变量容器和设计模板。 
- 渲染器- 处理来自视图模型的数据和模板并输出设计表示,可能是最终的 html 输出。 
- 解析器- 以渲染器可以使用的方式解析视图模型中可用的模板。 
- View (Zend\View\View) - 将请求映射到渲染器,然后将渲染器映射到响应。 
- 渲染策略- 视图使用它来将请求映射到渲染器。 
- 响应策略- 视图使用它来将渲染器映射到响应。 
视图层View处理ViewModel ,使用Resolver解析模板,使用Rendering Strategy渲染它,最后使用Response Renderer输出它。
视图层配置
与控制器一样,视图层可以在名为module.config.php的模块配置文件中进行配置。主要配置是指定模板的放置位置。这可以通过在“module.config.php”中添加以下配置来完成。
'view_manager' => [ 'template_path_stack' => ['tutorial' => __DIR__ . '/../view',], ]
默认情况下,视图层的所有组件都有默认Behave。例如,ViewModel通过“lowercase-module-name/lowercase-controller-name/lowercase-action-name”规则解析模板根中控制器操作的模板名称。但是,这可以被ViewModel 的setTemplate()方法覆盖。
控制器和视图层
默认情况下,控制器不需要向视图层发送任何数据。将模板写在适当的地方就足够了。
例如,在我们的示例中,TutorialController,模板需要放置在myapp/module/Tutorial/view/tutorial/tutorial/index.phtml中。index.phtml引用基于 PHP 的模板,它将由PHPRenderer 渲染。还有其他渲染器,例如用于json输出的JsonRenderer和用于rss和atom输出的FeedRenderer。
完整列表如下 -
<?php  
namespace Tutorial\Controller;  
use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel;  
class TutorialController extends AbstractActionController { 
   public function indexAction() { 
   } 
}
Zend 应用程序模板
<div class = "row content"> <h3>This is my first Zend application</h3> </div>
最后,我们成功完成了教程模块,我们可以使用 url – http://localhost:8080/tutorial访问它。
 
将数据传递到视图层
将数据发送到视图层的最简单方法是使用ViewModel参数。更改后的indexAction方法如下 -
public function indexAction() { 
   $view = new ViewModel([ 
      'message' => 'Hello, Tutorial' 
   ]);  
   return $view; 
} 
现在,更改index.phtml文件如下 -
<div class = "row content"> <h3>This is my first Zend application</h3> <h4><?php echo $this->message?></h4> </div>
查看助手
视图助手用于编写要在模板中使用的小型Atomics函数。Zend 框架提供了一个接口 Zend\View\Helper\HelperInterface 来编写标准视图助手。
HelperInterface 只有两个方法,
- setView() - 此方法接受 Zend\View\Renderer\RendererInterface 实例/实现。 
- getView() - 用于检索该实例。 
HelperInterface的完整代码清单如下 -
namespace Zend\View\Helper;  
use Zend\View\Renderer\RendererInterface as Renderer;  
interface HelperInterface { 
   /** 
      * Set the View object 
      * 
      * @param  Renderer $view 
      * @return HelperInterface 
   */ 
   public function setView(Renderer $view);  
   /** 
      * Get the View object 
      * 
      * @return Renderer 
   */ 
   public function getView(); 
}
要在视图脚本中使用助手,请使用$this->helperName()访问它。
内置助手
Zend Framework 提供了许多用于各种目的的内置帮助函数。zend-mvc中可用的一些视图助手如下 -
网址
URL 帮助器用于生成与应用程序中定义的路由匹配的 URL。
URL 助手的定义是 -
$this->url($name, $params, $options, $reuseMatchedParameters)
例如,在教程模块中,路由命名为tutorial,它有两个参数action和id。我们可以使用 URL 帮助器生成两个不同的 URL,如下所示 -
<a href = "<? = $this->url('tutorial'); ?>">Tutorial Index</a>  
<a href = "<? = $this->url('tutorial', ['action' => 'show', 'id' =>10]); ?>"> 
   Details of Tutorial #10 
</a>
结果如下 -
<a href = "/tutorial">Tutorial Index</a> <a href = "/tutorial/show/10"> Details of Tutorial #10</a>
占位符
占位符助手用于在视图脚本和视图实例之间保留内容。它提供了最初设置数据然后在后续阶段使用它的选项。
例如,我们可以设置公司名称,然后在所有其他地方使用它。
<?php $this->placeholder('companyname')->set("TutorialsPoint") ?>  
<?= $this->placeholder('companyname'); ?>
占位符提供了一些高级选项来从 PHP 数组和对象生成复杂的内容。它还可以选择捕获模板本身的某些部分。
例如,以下代码捕获之间的模板结果并将其存储在产品列表占位符中。
类别 – 产品
class Product { 
   public $name; 
   public $description; 
} 
控制器
$p1 = new Product(); $p1->name = 'Car'; $p1->description = 'Car'; $p2 = new Product(); $p2->name = 'Cycle'; $p2->description = 'Cycle'; $view = new ViewModel(['products' => $products]);
模板
<!-- start capture --> 
<?php $this->placeholder('productlist')->captureStart(); 
   foreach ($this->products as $product): ?> 
<div> 
   <h2><?= $product->name ?></h2> 
   <p><?= $product->description ?></p> 
</div> 
<?php endforeach; ?> 
<?php $this->placeholder('productlist')->captureEnd() ?> 
<!-- end capture -->  
<?= $this->placeholder('productlist') ?> 
结果
<div class = "foo"> <h2>Car</h2> <p>Car</p> </div> <div class = "foo"> <h2>Cycle</h2> <p>Cycle</p> </div>
文档类型
Doctype 帮助器用于生成各种 html 文档类型。它是占位符助手的具体实现。可以在引导文件和配置文件中设置文档类型。
基本用法如下所示 -
应用程序引导文件
use Zend\View\Helper\Doctype;  
$doctypeHelper = new Doctype(); 
$doctypeHelper->doctype('XHTML5'); 
模块配置
// module/Application/config/module.config.php: 
return [ 
   /* ... */ 
   'view_manager' => [ 
      'doctype' => 'html5', 
      /* ... */ 
   ], 
]; 
模板
<?php echo $this->doctype() ?>
标题
HeadTitle 帮助器用于生成 html 标题元素。它是Placeholder helper的具体实现。Zend 提供了一个在模块配置文件中设置标题的选项,它可以在任何级别设置,如站点、模块、控制器、操作等。HeadTitle 的部分代码如下 -
模块
headTitleHelper->append($action); $headTitleHelper->append($controller); $headTitleHelper->append($module); $headTitleHelper->append($siteName);
模板
<?= $this->headTitle() ?>
结果
action - controller - module - Zend Framework
头元
HeadMeta 帮助器用于生成 html 元标记。它是占位符助手的具体实现。
模板-
<?php 
   $this->headMeta()->appendName('keywords', 'turorialspoint, zend framework, php');  
   echo $this->headMeta() 
?>
结果
<meta name = "keywords" content = "tutorialspoint, zend framework, php" />
头联
HeadLink 帮助程序用于生成 html 链接以包含外部资源。它是占位符助手的具体实现。
模板
<?php 
   // setting links in a view script: 
   $this->headLink(['rel' => 'icon', 'href' => '/img/favicon.ico'], 'PREPEND') 
      ->appendStylesheet('/styles/site.css') 
      ->prependStylesheet('/styles/mystyle.css', 'screen', true, ['id' => 'mystyle']);  
   
   // rendering the links from the layout: 
   echo $this->headLink(); 
?>
结果
<link href = "/styles/mystyle.css" media = "screen" rel = "stylesheet" type = "text/css" id = "mystyle"> <link href = "/img/favicon.ico" rel = "icon"> <link href = "/styles/site.css" media = "screen" rel = "stylesheet" type = "text/css">
头型
HeadStyle 帮助器用于生成内联 CSS 样式。它是占位符助手的具体实现。
模板
<?php $this->headStyle()->appendStyle($styles); ?> <?php echo $this->headStyle() ?>
头脚本
HeadScript 用于生成内联脚本或包含外部脚本。它是占位符助手的具体实现。
模板
<? $this->headScript()->appendFile(‘/js/sample.js’);?> <?php echo $this->headScript() ?>
内联脚本
InlineScript 用于在 html 模板的 head 和 body 部分生成脚本。它源自 HeadScript。
HTML列表
HTMLList 用于生成有序和无序列表。HTMLList 的定义如下 -
定义
htmlList($items, $ordered, $attribs, $escape)
模板
$items = [ '2015', ['March', 'November'], '2016', ]; echo $this->htmlList($items);
结果
<ul> 
   <li>2015 
      <ul> 
         <li>March</li> 
         <li>November</li> 
      </ul> 
   </li> 
   <li>2016</li> 
</ul>
循环
Cycle 用于在循环环境中生成替代方案。它具有分配、下一个和上一个功能。
控制器
$view = new ViewModel(['message' => 'Hello, Tutorial', 'data' => array('One', 'Two')]);
模板
<?php $this->cycle()->assign(['#F0F0F0', '#FFF'], 'colors'); ?>
<table>
   <?php foreach ($this->data as $datum): ?>
   <tr style = "background-color: <?= $this->cycle()->setName('colors')>next() ?>">
      <td><?= $this->escapeHtml($datum) ?></td>
   </tr>
   <?php endforeach ?>
</table>
结果
<table> 
   <tr style = "background-color: #F0F0F0"> 
      <td>One</td> 
   </tr> 
   <tr style = "background-color: #FFF"> 
      <td>Two</td> 
   </tr> 
</table>
其他一些重要的内置助手如下 -
- BasePath - BasePath 用于生成应用程序根目录的公共文件夹的路径。 
- 部分- 部分用于在其自己的变量范围内呈现特定模板。 
- PartialLoop - PartialLoop 与 Partial 类似,但在循环环境中使用。 
- 身份- 身份用于从身份验证服务检索登录用户的身份。 
- JSON - JSON 在安静的环境中使用,输出为 JSON 格式。它发出正确的 HTTP 标头并禁用布局概念。 
Zend Framework 中仍然有很多可用的帮助程序,例如i18n 帮助程序、表单帮助程序、分页帮助程序、导航帮助程序等。
创建视图助手
Zend Framework 提供了一个内置的AbstractHelper实现HelperInterface来编写视图助手。
编写新助手的步骤如下 -
- 步骤 1 - 扩展类 Zend\View\Helper\AbstractHelper。 
- 步骤 2 - 覆盖__invoke()函数。 
- 步骤 3 - 在module.config.php 文件中设置配置。 
- 步骤 4 - 在视图脚本中使用视图助手。 
现在让我们创建一个TestHelper
在myapp/module/Tutorial/src/View 目录中创建 Helper 文件夹。在 Helper 目录中编写TestHelper TestHelper.php。
完整列表如下 -
<?php  
namespace Tutorial\View\Helper; 
use Zend\View\Helper\AbstractHelper; 
class TestHelper extends AbstractHelper { 
   public function __invoke() { 
      $output = "I am from test helper"; 
      return htmlspecialchars($output, ENT_QUOTES, 'UTF-8'); 
   } 
}
在module.config.php中设置配置。
'view_helpers' => [ 
   'aliases' => [ 
      'testHelper' => View\Helper\TestHelper::class, 
   ], 
   'factories' => [ 
      View\Helper\TestHelper::class => InvokableFactory::class, 
   ],
], 
在about视图脚本中使用新创建的TestHelper。
<?= $this->testHelper() ?>