Question

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?

Was it helpful?

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

OTHER TIPS

You could make use of a session and store the breadcrumbs there. You'd probably want to limit yourself to say 10 pages though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top