Zend Framework MVC uses layouts and views to render pages in the web browser; the overall page content is controlled by the layout specification, and the view level information is contained in the views. The concept is to minimize the amount of redundant HTML code that needs to be generated for each of these views.
By using layouts, the application can have a consistent user interface, which is also easy to customize; the views offer the flexibility to modify the targeted content and allow customization to the maximum possible extent. This is also known as two-step view.
When a new view is generated, the appropriate layout is identified from the layout definitions in the view_manager configuration and the view is rendered with that layout.
<HTML>
<HEAD>
.....
</HEAD>
<BODY>
......
<DIV id='content'>
........
</DIV>
....... FOOTER ...
</HTML>
Layout
View
The preceding schematic explains how the layout and view are combined to form an HTML page, so for each and every view, the view part changes and the layout part remains static.
View helpers
Zend Framework 2 offers a wide range of view helpers that help us perform complex operations on views; if the included helpers are not sufficient, you can define your own custom helper by implementing the interfaceZend\View\HelperInterface.
In this section, we will quickly review some of the included helpers in Zend Framework 2.
The URL helper
The syntax for this helper is url($name, $urlParams, $routeOptions = array(),
$reuseMatchedParams = array()).
The URL helper is used to generate the URL for a specific route. The route's segment match parameters can be passed over the URL helper to form a URL based on the route option; for example, see the following:
<a href="<?php $this->url('users/upload-manager', array('action'=>'edit', 'id' => 10));">Edit</a>
This code will generate <a href="/users/upload-manager/edit/10">Edit</a> if
the route definition is as follows:
'route' => '/user-manager[/:action[/:id]]'
The syntax for this helper is basePath().
The BasePath helper returns the base URL of the view, this can be used by developers to prepend to their custom URLs and form links for various resources.
The JSON helper
The syntax for this helper is json($jsonData = array()).
The JSON helper is used to render PHP arrays as JSON-encoded data. Most AJAX libraries classify JSON content by its content header, and this helper also sets the content type header to application/json.
Concrete placeholder implementations
Zend Framework makes use of placeholder helpers to perform some standard operations on the HTML head sections including adding/removing references to new JavaScript libraries, linking with new styles, adding and crossreferencing scripts, and adding/removing HTML head section's meta content.
This is achieved by the following list of helpers called as concrete placeholder helpers. The reason why they are called placeholder helpers is because the helpers themselves don't make any changes to the way in which the content isrendered. For example, if you add <?php
echo $this->headLink(); ?> to the HTML code, this won't do anything, until you add something to the headLink helper by using appendStylesheet or some other function.
The HeadLink helper
The HeadLink helper is used to modify the <link> tag in the HTML head section; this helper is used to attach or manage external CSSs.
Some of the most-used functions in this helper are listed as follows:
◆ appendStylesheet($href, $media, $conditionalStylesheet, $extras)
◆ offsetSetStylesheet($index, $href, $media,
$conditionalStylesheet, $extras)
◆ prependStylesheet($href, $media, $conditionalStylesheet,
$extras)
◆ setStylesheet($href, $media, $conditionalStylesheet, $extras)
The HeadMeta helper
The HeadMeta helper is used to modify the <meta> tag in the HTML head section; this helper is used to manipulate the HTML meta information.
Some of the most-used functions in this helper are listed as follows:
◆ appendName($keyValue, $content, $conditionalName)
◆ offsetSetName($index, $keyValue, $content, $conditionalName)
◆ prependName($keyValue, $content, $conditionalName)
◆ setName($keyValue, $content, $modifiers)
◆ appendHttpEquiv($keyValue, $content, $conditionalHttpEquiv)
◆ offsetSetHttpEquiv($index, $keyValue, $content,
$conditionalHttpEquiv)
◆ prependHttpEquiv($keyValue, $content, $conditionalHttpEquiv)
◆ setHttpEquiv($keyValue, $content, $modifiers)
◆ setCharset($charset)
The HeadScript helper
The HeadScript helper is used to modify the <script> tag in the HTML head section; this helper is used to attach external JavaScript and also add the <script> tags to the HTML head section.
Some of the most-used functions in this helper are listed as follows:
◆ appendFile($src, $type = 'text/javascript', $attrs = array())
◆ offsetSetFile($index, $src, $type = 'text/javascript', $attrs = array())
◆ prependFile($src, $type = 'text/javascript', $attrs = array())
◆ setFile($src, $type = 'text/javascript', $attrs = array())
◆ appendScript($script, $type = 'text/javascript', $attrs = array())
$attrs = array())
◆ prependScript($script, $type = 'text/javascript', $attrs = array())
◆ setScript($script, $type = 'text/javascript', $attrs = array())
The HeadStyle helper
The HeadStyle helper is used to modify the <style> tag in HTML head section; this helper is used to add internal styles by adding the <style> tags to the HTML head section.
Some of the most-used functions in this helper are listed as follows:
◆ appendStyle($content, $attributes = array())
◆ offsetSetStyle($index, $content, $attributes = array())
◆ prependStyle($content, $attributes = array())
◆ setStyle($content, $attributes = array())
Không có nhận xét nào:
Đăng nhận xét