Zend Framework navigation with breadcrumbs

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.

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>index</controller>
        <action>index</action>
        <pages>
            <xml>
                <label>Xml</label>
                <controller>xml</controller>
                <action>index</action>
                <pages>
                    <sample>
                        <label>Sample</label>
                        <controller>xml</controller>
                        <action>sample</action>
                    </sample>
                    <test>
                        <label>Test</label>
                        <controller>xml</controller>
                        <action>test</action>
                        <params>
                            <id>100</id>
                        </params>
                    </test>
                </pages>
            </xml>
            <sitemap>
                <label>Sitemap</label>
                <controller>sitemap</controller>
                <action>index</action>
                <pages>
                    <categories>
                        <label>Categories sitemap</label>
                        <controller>sitemap</controller>
                        <action>categories</action>
                    </categories>
                    <products>
                        <label>Products sitemap</label>
                        <controller>sitemap</controller>
                        <action>products</action>
                    </products>
                </pages>
            </sitemap>
        </pages>
    </nav>
</configdata>

In Boostrap.php class you need to initialize your navigation.
– xml way tells where your navigation.xml file is:

protected function _initNavigationXml()
{
    $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>
Advertisements

One thought on “Zend Framework navigation with breadcrumbs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s