Using zend-navigation in your Album Module
Using zend-navigation in your Album Module
In this tutorial we will use the zend-navigation component to add a navigation menu to the black bar at the top of the screen, and add breadcrumbs above the main site content.
Preparation
In a real world application, the album browser would be only a portion of a working website. Usually the user would land on a homepage first, and be able to view albums by using a standard navigation menu. So that we have a site that is more realistic than just the albums feature, lets make the standard skeleton welcome page our homepage, with the /album route still showing our album module. In order to make this change, we need to undo some work we did earlier. Currently, navigating to the root of your app (/) routes you to theAlbumController's default action. Let's undo this route change so we have two discrete entry points to the app, a home page, and an albums area.
// In module/Application/config/module.config.php:
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class, // <-- change back here
'action' => 'index',
],
],
],
(You can also now remove the import for theAlbum\Controller\AlbumController class.)
This change means that if you go to the home page of your application (http://localhost:8080/ or http://zf2-tutorial.localhost/), you see the default skeleton application introduction. Your list of albums is still available at the /album route.
Setting Up zend-navigation
First, we need to install zend-navigation. From your root directory, execute the following:
$ composer require zendframework/zend-navigation
Assuming you followed the Getting Started tutorial, you will be prompted by the zend-component-installer plugin to inject Zend\Navigation; be sure to select the option for either config/application.config.php orconfig/modules.config.php; since it is the only package you are installing, you can answer either "y" or "n" to the "Remember this option for other packages of the same type" prompt.
Manual configuration
If you are not using zend-component-installer, you will need to setup configuration manually. You can do this in one of two ways:
- Register the
Zend\Navigationmodule in eitherconfig/application.config.phporconfig/modules.config.php. Make sure you put it towards the top of the module list, before any modules you have defined or third party modules you are using.- Alternately, add a new file,
config/autoload/navigation.global.php, with the following contents:<?php
use Zend\Navigation\ConfigProvider; return [
'service_manager' => (new ConfigProvider())->getDependencyConfig(),
];
Once installed, our application is now aware of zend-navigation, and even has some default factories in place, which we will now make use of.
Configuring our Site Map
Next up, we need zend-navigation to understand the hierarchy of our site. To do this, we can add a navigation key to our configuration, with the site structure details. We'll do that in the Application module configuration:
// in module/Application/config/module.config.php:
return [
/* ... */
'navigation' => [
'default' => [
[
'label' => 'Home',
'route' => 'home',
],
[
'label' => 'Album',
'route' => 'album',
'pages' => [
[
'label' => 'Add',
'route' => 'album',
'action' => 'add',
],
[
'label' => 'Edit',
'route' => 'album',
'action' => 'edit',
],
[
'label' => 'Delete',
'route' => 'album',
'action' => 'delete',
],
],
],
],
],
/* ... */
];
This configuration maps out the pages we've defined in our Album module, with labels linking to the given route names and actions. You can define highly complex hierarchical sites here with pages and sub-pages linking to route names, controller/action pairs, or external uris. For more information, see thezend-navigation quick start.
Adding the Menu View Helper
Now that we have the navigation helper configured by our service manager and merged config, we can add the menu to the title bar to our layout by using themenu view helper:
<?php // in module/Application/view/layout/layout.phtml: ?>
<div class="collapse navbar-collapse">
<?php // add this: ?>
<?= $this->navigation('navigation')->menu() ?>
</div>
The navigation helper is provided by default with zend-view, and uses the service manager configuration we've already defined to configure itself automatically. Refreshing your application, you will see a working menu; with just a few tweaks however, we can make it look even better:
<?php // in module/Application/view/layout/layout.phtml: ?>
<div class="collapse navbar-collapse">
<?php // update to: ?>
<?= $this->navigation('navigation')
->menu()
->setMinDepth(0)
->setMaxDepth(0)
->setUlClass('nav navbar-nav') ?>
</div>
Here we tell the renderer to give the root <ul> the class of nav (so that Bootstrap styles the menu correctly), and only render the first level of any given page. If you view your application in your browser, you will now see a nicely styled menu appear in the title bar.
The great thing about zend-navigation is that it integrates with zend-router in order to highlight the currently viewed page. Because of this, it sets the active page to have a class of active in the menu; Bootstrap uses this to highlight your current page accordingly.
Adding Breadcrumbs
Adding breadcrumbs follows the same process. In our layout.phtml we want to add breadcrumbs above the main content pane, so our users know exactly where they are in our website. Inside the container <div>, before we output the content from the view, let's add a breadcrumb by using the breadcrumbs view helper.
<?php // module/Application/view/layout/layout.phtml: ?>
<div class="container">
<?php // add the following line: ?>
<?= $this->navigation('navigation')->breadcrumbs()->setMinDepth(0) ?>
<?= $this->content ?>
</div>
This adds a simple but functional breadcrumb to every page (we tell it to render from a depth of 0 so we see all page levels), but we can do better than that! Because Bootstrap has a styled breadcrumb as part of its base CSS, let's add a partial that outputs the <ul> using Bootstrap styles. We'll create it in the viewdirectory of the Application module (this partial is application wide, rather than album specific):
<?php // in module/Application/view/partial/breadcrumb.phtml: ?>
<ul class="breadcrumb">
<?php
// iterate through the pages
foreach ($this->pages as $key => $page):
?>
<li>
<?php
// if this isn't the last page, add a link and the separator:
if ($key < count($this->pages) - 1):
?>
<a href="<?= $page->getHref(); ?>"><?= $page->getLabel(); ?></a>
<?php
// otherwise, output the name only:
else:
?>
<?= $page->getLabel(); ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
Notice how the partial is passed a Zend\View\Model\ViewModel instance with thepages property set to an array of pages to render.
Now we need to tell the breadcrumb helper to use the partial we have just written:
<?php // in module/Application/view/layout/layout.phtml: ?>
<div class="container">
<?php // Update to: ?>
<?= $this->navigation('navigation')
->breadcrumbs()
->setMinDepth(0)
->setPartial('partial/breadcrumb') ?>
<?= $this->content ?>
</div>
Refreshing the page now gives us a styled set of breadcrumbs on each page.
Using zend-navigation in your Album Module的更多相关文章
- Using zend-paginator in your Album Module
Using zend-paginator in your Album Module TODO Update to: follow the changes in the user-guide use S ...
- Introducing the Blog Module
Introducing the Blog Module Now that we know about the basics of the zend-mvc skeleton application, ...
- Zend框架2入门(二) (转)
Zend框架2使用一个模块系统,和你组织内每个你的主应用程序特定代码模块.骨架提供的应用程序模块是用于提供引导,错误和路由配置到整个应用程序.它通常是用来提供应用水平控制器,比如说,应用程序的主页,但 ...
- ZendFramework-2.4 源代码 - 关于Module - 模块入口文件
<?php // /data/www/www.domain.com/www/module/Album/Module.php namespace Album; use Zend\ModuleMan ...
- Zend Framework 2中如何使用Service Manager
end Framework 2 使用ServiceManager(简称SM)来实现控制反转(IoC).有很多资料介绍了service managers的背景,我推荐大家看看this blog post ...
- Android Jetpack Navigation基本使用
Android Jetpack Navigation基本使用 本篇主要介绍一下 Android Jetpack 组件 Navigation 导航组件的 基本使用 当看到 Navigation单词的时候 ...
- ZendFramework-2.4 源代码 - 关于配置
$applicationConfig = $serviceManager->setService('ApplicationConfig'); // 获取配置 /data/www/www.doma ...
- ZendFramework-2.4 源代码 - 关于服务管理器
// ------ 决定“服务管理器”配置的位置 ------ // 1.在模块的入口类/data/www/www.domain.com/www/module/Module1/Module.php中实 ...
- Unit Testing a zend-mvc application
Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...
随机推荐
- JQuery focus()
跳转到登陆页面时可以使用focus方法 <input name="login_account"id="login_account"class=" ...
- jsp、js、html等
1.一个button标签怎么触发事件: 一般触发事件有两种方式,要么是在html直接绑定,即button标签中不只有class.type和id,还要写onclick=... 还有一种,就是在js代码部 ...
- Java Script 正则表达式的使用示例
一.语法 1.1 在JS中的使用代码 var myregex = new RegExp("^[-]?[0-9][0-9]{0,2}\\.[0-9]{5,15}\\,\s*[-]?[0-9][ ...
- 【译】 AWK教程指南 7AWK应用实例
本节将示范一个统计上班到达时间及迟到次数的程序. 这程序每日被执行时将读入两个数据文件: * 员工当日到班时间的数据文件 ( 如下列的 arr.dat ) * 存放员工当月迟到累计次数的文件 当程序执 ...
- javascript——继承
内容: 1.继承的概念.继承分为那几种继承及各种继承的区别 2.js中有那几种继承方式及各种继承的优缺点 3.总结
- HW6.18
public class Solution { public static void main(String[] args) { double[] array = {6.0, 4.4, 1.9, 2. ...
- HDU3966-Aragorn's Story(树链剖分)
第一道树链剖分. 早就想学..一直懒.. 感觉还是比较简单的. 主要是要套其他数据结构,线段树大概还好,平衡树之类的肯定就跪了. http://blog.csdn.net/acdreamers/art ...
- Java之序列流SequenceInputStream
序列流:作用就是将多个读取流合并成一个读取流,实现数据的合并 序列流表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到文件的末尾,接着从第二个输入流读取,以此类推:这样 ...
- 锋利的jquery第二版学习笔记
jquery系统学习笔记 一.初识:jquery的优势:1.轻量级(压缩后不到30KB)2.强大的选择器(支持css1.css2选择器的全部 css3的大部分 以及一些独创的 加入插件的话还可支持XP ...
- C#- 将秒数转化成任意时间格式
将秒数转化成任意时间格式,可以使用C#的一个函数TimeSpan,看示例: TimeSpan ts = new TimeSpan(0, 0, 3661); richTextBox2.Text = ts ...