zend framework 2 Dynamic Breadcrumbs - Passing Par

2019-06-27 23:40发布

问题:

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

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.

Example:

// 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'); ?>

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

Home > Sitemap

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

On the module.config.php the routing is..

<?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,
                ),
            ),

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

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]',

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

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

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

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

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

回答1:

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

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

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