Last Updated: February 25, 2016
·
1.361K
· joelstahre

Navigation in Zend Framework 1

A simple example of Navigation in zend framework 1

Create a xml file in application/configs/

ex. application/configs/navigation.xml

<?xml version="1.0" encoding="UTF-8"?>
<configdata>
    <nav>
        <home>
            <label>Home</label>
            <controller>index</controller>
            <action>index</action>
        </home>

        <about>
            <label>About</label>
            <controller>index</controller>
            <action>about</action>
        </about>

        <contact>
            <label>Contact</label>
            <controller>index</controller>
            <action>contact</action>
        </contact>

        <settings>
            <label>Settings</label>
            <controller>index</controller>
            <action>settings</action>
        </settings>


    </nav>
</configdata>

Add this to 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', 'nav');

        $navigation = new Zend_Navigation( $config );
        $view->navigation( $navigation );
 }

Add this to a view script or a layout script, ex. application/layouts/myLayout.phtml


<nav class="myNav">

    <?php echo $this->navigation()->menu(); ?>

</nav>



// ->setUlClass('nav'); sets a custom CSS class for the navigation
<?php echo $this->navigation()->menu()->setUlClass('nav'); ?>