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

Posted on: Jun 10, 2009 by wenbert

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

And what the breadcrumbs will look like:

//layout.phtml
echo $this->navigation()->breadcrumbs()->setLinkLast(false)->setMinDepth(0)->render();

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.


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

Browse Timeline


Comments ( 25 )

[...] 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: breadcrumbs, PHP, view helpers, Zend [...]

eKini: Web Developer Blog » Zend Framework: Making the Built-in Breadcrumb Helper Work added these pithy words on Jun 10 09 at 2:33 PM

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

what would be caused??
– rakesh

rakesh added these pithy words on Jul 13 09 at 5:23 PM

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

Wenbert added these pithy words on Jul 14 09 at 9:55 AM

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

rakesh added these pithy words on Jul 14 09 at 5:02 PM

@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 >
Wenbert added these pithy words on Jul 14 09 at 10:06 PM

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

rakesh added these pithy words on Jul 15 09 at 1:28 PM

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

rakesh added these pithy words on Jul 15 09 at 3:17 PM

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

rakesh added these pithy words on Jul 15 09 at 3:21 PM

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.

Wenbert added these pithy words on Jul 15 09 at 5:26 PM

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

Andrew added these pithy words on Aug 14 09 at 1:33 PM

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!

Jon Lebensold added these pithy words on Aug 20 09 at 11:11 PM

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

Wenbert added these pithy words on Aug 21 09 at 1:08 AM

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.

bobby added these pithy words on Sep 18 09 at 12:51 AM

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?

Bobby added these pithy words on Sep 22 09 at 9:38 PM

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?

sudoku added these pithy words on Oct 15 09 at 3:33 PM

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?

sudoku added these pithy words on Oct 15 09 at 3:34 PM

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

superp added these pithy words on Nov 14 09 at 5:43 PM

Hi

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

cheers

Pradosh added these pithy words on Nov 30 09 at 9:50 AM

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

Wenbert added these pithy words on Nov 30 09 at 9:56 AM

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

Regards,

Richard

Richard added these pithy words on Dec 31 09 at 5:07 AM

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.

Peter J. Schoenster added these pithy words on Jan 22 10 at 1:30 AM

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??

PixelMaker added these pithy words on Mar 19 10 at 5:27 AM

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.

Wenbert added these pithy words on Mar 19 10 at 2:56 PM

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

cwhisperer added these pithy words on Apr 16 10 at 3:56 PM

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

Wenbert added these pithy words on Apr 16 10 at 5:57 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!