Zend Framework double class menu active

元气小坏坏 提交于 2019-12-11 08:18:43

问题


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,


回答1:


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!



来源:https://stackoverflow.com/questions/8169382/zend-framework-double-class-menu-active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!