Zend Framework: Making the Built-in Breadcrumb Helper Work

Posted on: May 25, 2009 by wenbert

In the latest release of Zend Framework (1.8.1 as of this writing), there is a new built-in view helper to render your breadcrumb needs. There is a section in the documentation that tells us how to use it. But I got a little confused when I tried to make it work.

So, here is a short example on how I made my breadcrumbs work using the new breadcrumb view helper from Zend Framework.

First off, I want to render these links:

Before you are able to render your breadcrumb, you must create a container for it first. For the breadcrumbs above, you can have something that will look like this:

// I have placed this code somewhere in:
// application/layouts/scripts/layout.phtml
$container = new Zend_Navigation();
$this->navigation($container);
 
$container->addPage(
    array(
        'label'      => 'Dashboard',
        'module'     => 'default',
        'controller' => 'dashboard',
        'action'     => 'index',
        'pages'      =>
        array(
            array(
                'label'      => 'Create Order',
                'module'     => 'default',
                'controller' => 'createorder',
                'action'     => 'index'
            ),
            array(
                'label'      => 'Query',
                'module'     => 'default',
                'controller' => 'query',
                'action'     => 'index',
                'pages' => array(
                    array(
                        'label'      => 'View Order',
                        'module'     => 'default',
                        'controller' => 'order',
                        'action'     => 'vieworder'
                    )
                )
            ),
            array(
                'label'      => 'Administration',
                'module'     => 'default',
                'controller' => 'admin',
                'action'     => 'index',
                'pages'      =>
                array(
                    array(
                        'label'      => 'News and Announcements',
                        'module'     => 'default',
                        'controller' => 'admin',
                        'action' => 'addnews',
                        'pages' => array(
                            array(
                                'label'      => 'Edit News and Announcements',
                                'module'     => 'default',
                                'controller' => 'admin',
                                'action' => 'editnews'
                            )
                        )
                    )
                )
            )
        )
    )
);
 
//Finally, echo the breadcrumb!
echo '<div class="breadcrumbs">';
echo 'You are in: '.$this->navigation()->breadcrumbs()->setLinkLast(false)->setMinDepth(0)->render();
echo '</div>';

A breadcrumb should appear if you are in the pages specified in the $container array.

Update:
I have a new post using Zend_Navigation and breadcrumbs using an XML file. Using an XML file makes more sense than creating the huge array above. The post can be found here.


Subscribe to comments Comment | Trackback |
Post Tags: , , ,

Browse Timeline


Comments ( 12 )

[...] on his blog today Wenbert Del Rosario has posted a tutorial about getting the Zend_Navigation helper to work in your application and correctly handle [...]

Wenbert Del Rosario’s Blog: Zend Framework: Making the Built-in Breadcrumb Helper Work | Webs Developer added these pithy words on May 29 09 at 4:05 AM

[...] is related to the Making the Built-in Breadcrumb Helper Work I posted earlier. Thanks to Jonathan Lebensold’s screencast, I am able to create my [...]

eKini: Web Developer Blog » Zend Framework: Navigation and Breadcrumbs with an XML File in ZF 1.8 added these pithy words on Jun 10 09 at 2:06 PM

[...] it even includes view helpers to help us render links and menus and breadcrumbs! Yey! There are a several blog posts which go in details about Zend_Navigation, so I won’t be bothering with that. What [...]

Playing with Zend_Navigation and routes ~ Robert Basic added these pithy words on Aug 10 09 at 1:05 AM

THANK YOU!
I’ve been looking all over for a good explanation of that helper. I was just about to write my own (I had no trouble pulling the data out of the navigation() but ->breadcrumb()->render() was doing nothing)

MikeW added these pithy words on May 28 09 at 6:38 AM

@MikeW You’re welcome. I toyed with that helper for days myself — without any luck. Then I decided to ask the mailing list and then posted the code I made here. :P I’m glad you found it helpful!

Wenbert added these pithy words on May 28 09 at 9:29 AM

Hmm, is creating the page array by yourself the only option though? cant it be obtained like, in the controllers with the parameters and such?

selo added these pithy words on Jun 05 09 at 3:02 AM

@selo, you can also use an XML file. I’m sure it is found in the documentation.

Regarding the controllers, I have tried it yet, but you can probably create your own code to “generate” the array on the fly based on the controllers and actions you are currently in.

Wenbert added these pithy words on Jun 05 09 at 7:37 AM

to MikeW: If you generate a $container in plugin, you must use a preDispatch method.

Jiri added these pithy words on Jun 05 09 at 4:02 PM

Will an XML file work just as well?

AlanR08 added these pithy words on Aug 04 09 at 7:47 PM

Hi,

Did you ever think about creating your own blog? There are many excellent platforms, but by far the best is Wordpress. It is fast to set up, however the themes just never fit my specifications. I searched for a simple solution to this problem and realized that there wasn’t one. I then had a template custom made for my needs and was so happy with the outcome. I then decided to build a website that would show the world how to simply hire an expert in wordpress design.

[url=http://hirewordpressexperts.com/]Wordpress Expert[/url]

BaciaSims added these pithy words on Aug 23 09 at 1:36 PM

I’ve been looking all over for a good explanation of that helper. thanks a lot

vicky added these pithy words on Dec 04 09 at 8:26 PM

Add a Comment


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">


© Copyright 2007 eKini Web Developer Blog . Thanks for visiting!