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: Navigation and Breadcrumbs with an XML File in ZF 1.8

This 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 navigation and breadcrumbs in a better way. Using an XML file makes more sense than using an array. It is easier to maintain and edit.

What the Navigation will look like:

//layout.phtml
echo $this->navigation()->menu();
  • Home
  • Query

    • My Drafts
    • My Orders
    • All Open Orders
  • Reports

    • Country
    • Country Revenue

And what the breadcrumbs will look like:

//layout.phtml
echo $this->navigation()->breadcrumbs()->setLinkLast(false)->setMinDepth(0)->render();
  • You are in: Home
  • Home > Query

    • You are in: Home > Query > My Drafts
    • You are in: Home > Query > My Orders
    • You are in: Home > Query > All Open Orders
  • You are in: Home > Reports

    • You are in: Home > Reports > Country
    • You are in: Home > Reports > Country Revenue

To do this, you first create an XML file. It makes more sense to create your XML file inside the configs directory. So create an XML file in:

application/configs/navigation.xml

The data in the XML file should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<configdata>
    <nav>
        <label>Home</label>
        <controller>dashboard</controller>
        <action>index</action>
        <pages>
            <query>
                <label>Query</label>
                <controller>query</controller>
                <action>index</action>
                <pages>
                    <mydrafts>
                        <label>My Drafts</label>
                        <controller>query</controller>
                        <action>index</action>
                        <params>
                            <queryname>mydrafts</queryname>
                        </params>
                    </mydrafts>
                    <myorders>
                        <label>My Orders</label>
                        <controller>query</controller>
                        <action>index</action>
                        <params>
                            <queryname>myorders</queryname>
                        </params>
                    </myorders>
                    <allopenorders>
                        <label>All Open Orders</label>
                        <controller>query</controller>
                        <action>index</action>
                        <params>
                            <queryname>allopenorders</queryname>
                        </params>
                    </allopenorders>
                </pages>
            </query>
            <reports>
                <label>Reports</label>
                <controller>report</controller>
                <action>index</action>
                <pages>
                    <country>
                        <label>Country</label>
                        <controller>report</controller>
                        <action>country</action>
                    </country>
                    <countryrevenue>
                        <label>Country Revenue</label>
                        <controller>report</controller>
                        <action>countryrevenue</action>
                    </countryrevenue>
                </pages>
            </reports>
        </pages>
    </nav>
</configdata>

The next part involves editing the Bootstrap.php file.

//add this in your bootstrap file
//application/Bootstrap.php
protected function _initNavigation()
{
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();
    $config = new Zend_Config_Xml(APPLICATION_PATH.'/configs/navigation.xml');
 
    $navigation = new Zend_Navigation($config);
    $view->navigation($navigation);
}

What we are doing here is that we are simply loading the XML file (as an array) into the $view object. We are doing this so that we can access the Zend_Navigation object in our layout/views.

Final Steps
Lastly, you will need to open up your layout file and add these lines:

<html>
    <head>
    </head>
    <body>
        <div id="menu">
            <?php echo $this->navigation()->menu(); ?>
        </div>
        <div id="breadcrumbs">
            You are in: <?php echo $this->navigation()->breadcrumbs()->setLinkLast(false)->setMinDepth(0)->render(); ?>
        </div>
        <div id="content">
            Content of your page here.
        </div>
    </body>
<html>

More reading:
If you have time, check out Jonathan Lebensold’s screencast found in ZendCasts. It is an excellent tutorial and it goes all the way to beautifying your navigation using tabs and your breadcrumbs.

And lastly, the Zend_Navigation documentation can be found here.

This entry was posted in Uncategorized. Bookmark the permalink.

35 Responses to Zend Framework: Navigation and Breadcrumbs with an XML File in ZF 1.8

  1. Pingback: eKini: Web Developer Blog » Zend Framework: Making the Built-in Breadcrumb Helper Work

  2. rakesh says:

    hi,
    I created navigation.xml file. Menu i got but not able to see any breadcrumb.

    what would be caused??
    – rakesh

  3. Wenbert says:

    @rakesh, I will need more details on your problem. Any errors?

  4. rakesh says:

    thanks,
    That error is resolved. I had given wrong controller name :(

    Well, having other problem now.

    Right now my menu is like Home->page1, Home->page2.
    While viewing page1 or page2. breadcrumb is working correctly.
    My Page1 is a submit form which lead to other ‘thank you’ page.
    Now on my ‘thank you’ page my breadcrumb is not displaying.

    So can you suggest How to display my breadcrumb on also thank you page.

    – rakesh

  5. Wenbert says:

    @rakesh you will also need to put your “Thankyou” page in the XML… something like this:

    < thankyou>
         < label>Thanks< /label>
         < controller>thanks< /controller>
         < action>index< /action>
    < /thankyou >
    
  6. rakesh says:

    Thanks,
    but does it not display thanks as part of my menu. ??
    Ok let me try first yours solution ..

  7. rakesh says:

    Hi, Didn’t get may be something wrong with my XML file.
    Here is my xml file

    Home
    default
    index

    Error Correction
    index
    index

    Thanks
    thanks
    index

    Editing
    Edit
    index

    Race Start
    report
    country

    Entry Race Entry Start
    report
    countryrevenue

    Right now I have only one controller index

  8. rakesh says:

    and put my breadcrumb code in layout page. code of breadcrumb
    You are in: navigation()->breadcrumbs()->setLinkLast(false)->setMinDepth(0)->render(); ?>
    –rakesh

  9. Wenbert says:

    Hi rakesh, it is a bit hard to debug here. Please post your problem in ZF Mailing List: http://www.nabble.com/Zend-Framework-Community-f16154.html

    There are more people there who will reply and help.

  10. Andrew says:

    Thanks for this. Very clear and concise instructions. Very helpful!

  11. hey Wenbert,

    thanks for mentioning the query_params – I overlooked this in my screencast. Your tutorial saved me a good 30 minutes of reading / googling!

  12. Wenbert says:

    Thanks Jon. I’m glad that you found something useful here. I am a big fan of ZendCasts ;-)

  13. bobby says:

    How would we go about passing in parameters? I have a rental listings page that is narrowed down by region. I need the user to be able to return to the listings for that section when they click back on rentals from the rental details page.

    I must be overlooking something, surely this is a very common task. Without it there’s no benefit of using Zend for breadcrumbs.

  14. Bobby says:

    More specifically, how would I add variables as params, not hardcoded values in the XML file. Am I forced to use an array to accomplish this?

  15. sudoku says:

    Hi,

    how do i get the breadcrumbs to work if the navigation have also dynamic url this is in the database? It is not static like i see in the above example.

    So i can have
    http://www.daily-sudoku.com/dailysudoku/play-sudoku-online/solve-online-40.html

    with the number in the url changing? any idea?

  16. sudoku says:

    Hi,

    how do i get the breadcrumbs to work if the navigation have also dynamic url this is in the database? It is not static like i see in the above example.

    So i can have
    http://www.daily-sudoku.com/dailysudoku/play-sudoku-online/solve-online-40.html

    with the number in the url changing? any idea?

  17. superp says:

    sudoko…..same problem, have u solved it? need solution!

  18. Pradosh says:

    Hi

    How would this example work if the breadcrumbs need to be dynamic?

    cheers

  19. Wenbert says:

    Hello,

    This tutorial will not work for dynamic since you will need the XML file to render the breadcrumbs. I have also not gone into detail on Zend_Navigation. But you can by reading the documentation here: http://framework.zend.com/manual/en/zend.navigation.html

    Also, John Lebonsold has this screencast: http://www.zendcasts.com/zend_navigation-dynamically-creating-a-menu-a-sitemap-and-breadcrumbs/2009/06/

    Thanks,
    Wenbert

  20. Richard says:

    Thanks for the info and the links. Zend navigation is a hard topic, but well worth learning.

    Regards,

    Richard

  21. Peter J. Schoenster says:

    I like using the XML but I don’t see how to use dynamic variables in it. Who uses urls without dynamic content in them?

    This guys shows how to use navigation with dynamic variables:

    http://blog.ekini.net/2009/05/25/zend-framework-making-the-built-in-breadcrumb-helper-work/

    pretty straightforward.

  22. PixelMaker says:

    Hey,

    Nice article. Thanks for sharing it.

    I wonder how we can add ‘Logout’ at the end of the navigation if the user already logged in??

  23. Wenbert says:

    You can probably add it inside a span at the end of the list. I think there are a million ways to do it though.

  24. cwhisperer says:

    Hi,
    I tried the example in the cast and it works fine… I now have a backend module and the default which is the frontend in my website. Frontend and backend have different navigations. How can I solve this problem? With view helpers? If so, please submit an example… ;)
    Regards

  25. Wenbert says:

    @cwhisperer, sorry I do not have enough time to create the example you are asking for.

    You can probably solve your problem by having two XML files and using a view helper (with the conditions, etc.) to render one of each of the XML files.

  26. ano says:

    is there a way to make Navigation and Breadcrumbs with Database??

  27. Wenbert says:

    hello ano, have you tried to “generate” the XML file dynamically using the values from your database.

  28. Pingback: Breadcrumbs Navigations for "Two Step View layout"

  29. David Ruiz says:

    Excellent! Simply, well done.

  30. nana says:

    Hi,

    Do you have an idea how to use zend_acl together with zend_navigation so that only authorised users have access to certain menus and breadcrumbs. I seem to be following the tutorials I have found from searching google but nothing seems to work. Do you have any experience with that?

    Thanks

  31. Hi, Nice article. There is only a small bug in Warpturn_Plugin_Navigation plugin somewhere on line 66:

    return $this->_navigation should be self::$_navigation.

  32. Richard Ayotte says:

    If you want to make the navigation available to your regular view, you also need to add:

    $this->bootstrap(‘view’);
    $view = $this->getResource(‘view’);
    $view->navigation($navigation);

  33. Pingback: breadcrumbs problem

  34. vinay says:

    can we pass parameters in the xml file for breadcrumbs. sometimes we pass parameters with the link and when we come back by using the breadcrumbs then it will not work.

  35. Pingback: Zend Navigation Menu customising to use JQuery jdmenu

Leave a Reply to David Ruiz 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>