zend framework 2动态面包屑-传递参数 [英] zend framework 2 Dynamic Breadcrumbs - Passing Parameters

查看:121
本文介绍了zend framework 2动态面包屑-传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Zend Framework 2的新手,我正在尝试使用ZF2生成动态面包屑,但没有成功.

I'm a newcomer to Zend Framework 2 and I´m trying to generate dynamic Breadcrumbs using ZF2 but without success.

我使用了 http://adam.lundrigan .ca/2012/07/quick-and-dirty-zf2-zend-navigation/作为我工作的基础,并且如上所述,我为我的模块公开的路线构建了站点地图.

I used http://adam.lundrigan.ca/2012/07/quick-and-dirty-zf2-zend-navigation/ as a base for my work and as described I constructed the sitemap for the routes exposed by my modules.

示例:

// config/autoload/nav_zfcuser.global.php

<?php
return array(
    // All navigation-related configuration is collected in the 'navigation' key
    'navigation' => array(
        // The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
        'default' => array(
            // And finally, here is where we define our page hierarchy
            'home' => array(
                'label' => 'Home',
                'route' => 'home',
                'pages' => array(
                    'sitemap' => array(
                        'label' => 'Sitemap',
                        'title' => 'Sitemap',
                        'route' => 'sitemap',
                    ),
                    'aboutus' => array(...),
                    'privacy' => array(...),
                ),
            ),
        ),
    ),
);

然后使用以下视图助手来渲染面包屑:

Then to render the breadcrumbs used the following view helper:

<?php echo $this->navigation()->breadcrumbs('Navigation'); ?>

这很好用,例如,如果我在mywebsite.com/sitemap上导航,呈现的bredcrumbs将是

This works fine and for example if I navigate on mywebsite.com/sitemap the rendered bredcrumbs will be.

首页>网站地图

问题是当我开始使用动态网址(例如带有产品ID的网址)时.

The problem is when I start having dynamic URL´s (e.g. with product ID´s).

module.config.php 上,路由是..

<?php
return array(
    'router' => array(
        'routes' => array(

         (...)

  'productHome'  => array(
            'type'      => "Literal",
            'options'   => array(
                'route'     => "/product",
                'defaults'  => array(
                    '__NAMESPACE__' => "Application\Controller",
                    'controller'    => "Product",
                    'action'        => "index",
                ),
            ),
            'may_terminate' => true,
            'child_routes'  => array(
                'product'   => array(
                    'type'      => "Segment",
                    'options'   => array(
                        'route'     => "/:idProduct[/:name]",
                        'defaults'  => array(
                            '__NAMESPACE__' => "Application\Controller",
                            'controller'    => "Product",
                            'action'        => "product",
                        ),
                    ),
                    'may_terminate' => true,
                ),
            ),

上面定义的路由( module.config.php )转换为以下URL结构:

The routing defined above (module.config.php) translates into the following URL structure:

mysite.com/Product/:idProduct [/:name]

mysite.com/Product/:idProduct[/:name]

因此,构建反映此结构的站点地图应该是:

So constructing the sitemap to reflect this structure should be:

// config/autoload/nav_zfcuser.global.php 

 <?php
    return array(
        // All navigation-related configuration is collected in the 'navigation' key
        'navigation' => array(
            // The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
            'default' => array(
                // And finally, here is where we define our page hierarchy
                'home' => array(
                    'label' => 'Home',
                    'route' => 'home',
                    'pages' => array(
                        'sitemap' => array(...),
                        'productHome' => array(
                            'label' => 'Product',
                            'route' => 'product',
                            'controller' => 'Product',
                            'action' => 'index',
                            'pages' => array(
                                'product' => array(
                                'label' => 'Product',
                                'controller' => 'Product',
                                'action' => 'product',
                                'route' => 'product/:idProduct[/:name]',

                            ),
                        ),
                      ),
                    ),
                ),
            ),
        ),
    );

在浏览器 mywebsite.com/product/flv6n768/ProductName 上刷新产品页面. 没有显示面包屑.

Refreshing the products page on the browser mywebsite.com/product/flv6n768/ProductName no Breadcrumbs are displayed.

如何传递"idProduct"和"name"参数,以便在调用视图助手时使用

How can I pass 'idProduct' and 'name' params so that when I call the view helper

 <?php echo $this->navigation()->breadcrumbs('Navigation'); ?>  

它呈现:主页>产品> idProduct(不可见)>产品名称?

It renders: Home > Product > idProduct (not visible) > name of the product ?

推荐答案

如果仅出于面包屑的目的,您可以执行以下操作:

If it's for purpose of breadcrumbs only you can do the following:

按如下所示修改导航数组-删除特定的产品页面规格.并将ID添加到产品页面.

Modify your navigation array as below - remove specific product page spec. and add ID to products page.

<?php
return array(
    // All navigation-related configuration is collected in the 'navigation' key
    'navigation' => array(
        // The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
        'default' => array(
            // And finally, here is where we define our page hierarchy
            'home' => array(
                'label' => 'Home',
                'route' => 'home',
                'pages' => array(
                    'sitemap' => array(...),
                    'productHome' => array(
                        'id' => 'productHome',//<<< Page unique ID
                        'label' => 'Product',
                        'route' => 'product',
                        'controller' => 'Product',
                        'action' => 'index',
                    ),
                  ),
                ),
            ),
        ),
    ),
);

在Application \ Controller \ Product :: product()中,添加以下代码.这会将动态"新的子页面添加到导航静态结构中的产品索引页面.还要注意,我假设您在$ product变量中有可用的实际产品实例.

In Application\Controller\Product::product() add at the end the following code. This adds 'dynamically' new sub page to product index page in navigation static structure. Also notice that I presume you have got actual product instance available in $product variable.

    $navigation = $this->getServiceLocator()->get('Navigation');
    $page = $navigation->findBy('id', 'productHome');
    $page->addPage(array(
        'uri' => $this->getRequest()->getRequestUri(),//current page URI
        'label' => $product->getName(),//<<<<< product name
        'active' => true,
    ));

这篇关于zend framework 2动态面包屑-传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆