Search code examples
phpzend-frameworkzend-navigation

Zend Framework double class menu active


I make a menu with Zend_Navigation. The trouble is that I detect a few times "active menu" that is, the "li" of the current page.

Here is my navigation.xml

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

    <search>
        <label>Riads</label>
        <controller>search</controller>
        <action>index</action>
        <params>
            <q>allriads</q>
        </params>
    </search>

    <last>
        <label>Dernières Minutes</label>
        <uri>#</uri>
    </last>

    <promotion>
        <label>Promotions</label>
        <uri>#</uri>
    </promotion>

    <groupes>
        <label>Groupes</label>
        <uri>#</uri>
    </groupes>

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

</nav>

Here is the code in my bootstrap

/**
 * @return Zend_Navigation
 */
protected function _initNavigation()  
{
    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
    $this->_view->navigation(new Zend_Navigation($config));
    $activeNav = $this->_view->navigation()->findByController('index');
    $activeNav->active = true;
    $activeNav->setClass("active");
}

Here is the HTML generated

<ul class="navigation">
<li class="active">
    <a class="active" href="/v2/">Accueil</a>
</li>
<li class="active">
    <a href="/v2/search/index/q/allriads">Riads</a>
</li>

<li>
    <a href="#">Dernières Minutes</a>
</li>
<li>
    <a href="#">Promotions</a>
</li>
<li>
    <a href="#">Groupes</a>

</li>
<li>
    <a href="/v2/apropros/contact">Contact</a>
</li>
</ul>

The good code must be :

<ul class="navigation">
<li>
    <a href="/v2/">Accueil</a>
</li>
<li>
   <a href="/v2/search/index/q/allriads">Riads</a>
</li>
[...]

What is the solution?

Sincerely,


Solution

  • My solution

    change your Bootrasp.php:

    public function _initNavigation()
    {
        $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
        $navigation = new Zend_Navigation($config);
        Zend_Registry::set('Zend_Navigation', $navigation);
    }
    

    In your layout.phtml

    <?=$this->navigation()->menu()->renderPartial(null, 'shared/menu.phtml')?>
    

    in the partial file: /application/views/shared/menu.phtmt write this:

    <ul class="navigation">
        <?
        foreach ($this->container as $page) :
        /** @var $page Zend_Navigation_Page_Mvc */
        ?>
            <li class="<?=$page->isActive(true) ? 'active' : ''?>">
                <a href="<?=$page->getHref()?>"><b><?=$page->label?></b></a>
            </li>
        <? endforeach; ?>
    </ul>
    

    By doing this, it inhibits the html generated by Zend_Navigator, but you decide to generate the html! I hope to check out was of help!