A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Zend Framework: Making the Built-in Breadcrumb Helper Work

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:

  • You are in: Dashboard > Create Order
  • You are in: Dashboard > Query
  • You are in: Dashboard > Query > View Order
  • You are in: Dashboard > Administration
  • You are in: Dashboard > Administration > News and Announcements
  • You are in: Dashboard > Administration > News and Announcements > Edit News and Announcements

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.

This entry was posted in Uncategorized. Bookmark the permalink.

15 Responses to Zend Framework: Making the Built-in Breadcrumb Helper Work

  1. MikeW says:

    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)

  2. Wenbert says:

    @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!

  3. Pingback: Wenbert Del Rosario’s Blog: Zend Framework: Making the Built-in Breadcrumb Helper Work | Webs Developer

  4. selo says:

    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?

  5. Wenbert says:

    @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.

  6. Jiri says:

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

  7. Pingback: eKini: Web Developer Blog » Zend Framework: Navigation and Breadcrumbs with an XML File in ZF 1.8

  8. AlanR08 says:

    Will an XML file work just as well?

  9. Pingback: Playing with Zend_Navigation and routes ~ Robert Basic

  10. BaciaSims says:

    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]

  11. vicky says:

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

  12. Pingback: Zend_Navigation menu works but not breadcrumbs | DEEP IN PHP

  13. ecambodia says:

    Hi, I really thank for your post and it need what i found because I want to do with array. But when I implement with it and do it in layout page. It doesn’t show anything? Do i need to add other code beside above code? Please help me more.

  14. If you generate a $container in plugin, you must use a preDispatch method.Thanks

Leave a Reply to Wenbert Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>