zend framework 2动态面包屑 - 传递参数

时间:2013-07-26 14:59:18

标签: php routing navigation zend-framework2 breadcrumbs

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

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

示例:

// 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(...),
                ),
            ),
        ),
    ),
);

然后使用以下视图帮助器呈现面包屑:

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

这很好用,例如,如果我在mywebsite.com/sitemap上导航,那么渲染的bredcrumbs将是。

主页&gt;网站地图

问题在于我开始使用动态网址(例如,使用产品ID)。

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结构:

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

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

// 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 没有显示面包屑。

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

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

它呈现:Home&gt;产品&gt; idProduct(不可见)&gt;产品名称?

1 个答案:

答案 0 :(得分:5)

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

按如下所示修改导航阵列 - 删除特定产品页面规范。并将ID添加到产品页面。

<?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变量中获得实际的产品实例。

    $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,
    ));