zend-framework2zend-navigation

ZF2 Breadcrumbs with dynamic content


How can I create breadcrumbs with dynamic content in ZF2 using Zend Navigation and the Breadcrumbs view helper? For example:

Home -> People -> John Doe

In this case "John Doe" is the profile page you are currently viewing which is loaded with a uri like /people/23424 where the last parameter is the id of the person (there could be thousands).

How can I do this given that all the navigation pages are created in the service config?


Solution

  • Add nav to your service config:

    'navigation' => function($sm) {
        $navigation = new \Zend\Navigation\Service\DefaultNavigationFactory;
        $navigation = $navigation->createService($sm);
        /**
         * Do extra work here with the nav at instantiate time..
         */ 
     },
    

    or more simply:

    'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory'
    

    Now you can fetch the items you want to change like this:

    $nav = $this->getServiceLocator()->get('navigation');
    $page = $nav->findByLabel('My Label');
    $page->setLabel('New Label');