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\Navigation module in eitherconfig/application.config.php or config/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.

时间: 2024-08-12 11:56:07

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 SQLite-compatible SQL syntax, and provide a script for inserting the data In this tutorial, we will use the zend-paginator component to add a handy pag

module/config/module.config.php文件内涵定义

'router'                     =>array(),// 路由 'controllers'              =>array(),// 控制器 'view_manager'       =>array(),// 视图管理器 'service_manager'       =>array(),// 服务器管理器 'translator'              =>array(),// 译码器或翻译器 'navigation'        

菜菜鸟Zend Framework 2 不完全学习涂鸦(四)-- 模块

菜菜鸟Zend Framework 2 不完全学习涂鸦(四)-- 模块 这是涂鸦的第四篇 模块(Modules) ZF2 是一个模块系统,而你需要在每个模块中组织你主要应用代码.由模板(skeleton)所提供的应用程序模块在整个应用程序中被用作引导(bootstrapping),错误(error)和路由设置(routing configuration).它经常被用作提供应用级别控制,例如,应用程序的首页.但是在这个教程中我们不使用默认的模块,我们将使用唱片列表来作为应用程序的首页. 我们将代码

Zend Framework 2中如何使用Service Manager

end Framework 2 使用ServiceManager(简称SM)来实现控制反转(IoC).有很多资料介绍了service managers的背景,我推荐大家看看this blog post from Evan和 this post from Reese Wilson,但是仍然有很多开发者不能够很好地使用ServiceManager去解决他们的需求.这篇文章我将解释为什么ZF2框架需要使用多个服务管理器以及怎样使用它们.主要包含以下几个方面: 这些不同的服务管理器是什么? 不同的服务管

ZendFramework-2.4 源代码 - 关于配置

$applicationConfig = $serviceManager->setService('ApplicationConfig'); // 获取配置 /data/www/www.domain.com/www/config/application.config.php // -----------case.0------------------ // -----------服务管理器的配置------------------ $applicationConfig = array( // .

ZendFramework-2.4 源代码 - 关于MVC - View层 - 视图渲染器、视图插件管理器

<?php // 1. 视图渲染器 class PhpRenderer implements Renderer, TreeRendererInterface { /** * 插件管理器 */ public function getHelperPluginManager() { if (null === $this->__helpers) {// false $this->setHelperPluginManager(new HelperPluginManager()); } return

Unit Testing a zend-mvc application

Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in large projects, especially those with many people involved. Going back and manually testing every individual component of an application after every c

Database and models

Database and models The database Now that we have the Album module set up with controller action methods and view scripts, it is time to look at the model section of our application. Remember that the model is the part that deals with the application

Forms and actions

Forms and actions Adding new albums We can now code up the functionality to add new albums. There are two bits to this part: Display a form for user to provide details. Process the form submission and store to database. We will use zend-form to do th