zend框架2动态面包屑-通过参数


zend framework 2 Dynamic Breadcrumbs - Passing Parameters

我是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上导航,则渲染的bredcrumb将为。

主页>网站地图

问题是当我开始拥有动态URL时(例如产品ID)。

模块.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"answers"name"参数,以便在调用视图助手时

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

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

如果只用于面包屑,您可以执行以下操作:

如下修改您的导航阵列-删除特定的产品页面规范并将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,
    ));