如何更改zend导航中的子菜单活动类?

时间:2013-01-07 09:22:31

标签: php zend-framework zend-navigation


我正在使用zend导航来创建菜单。我想更改默认课程' active'选择'我还想将类添加到主动链接的锚标签。 这是我在bootstrap中添加的示例代码

protected function _initNavigation()
{

    $pages = array(
                        array(
                            'module' => 'admin',
                            'label' => 'Services',
                            'resource' => 'admin',
                            'controller' => 'services',
                            'pages' => array(
                                array(
                                    'module'        => 'admin',
                                    'label'         => 'Add Services',
                                    'controller'    => 'services',
                                    'action'        => 'manage',
                                    'route'         => 'default',

                                    ),
                                array(
                                    'module'        => 'admin',
                                    'label'         => 'View Services',
                                    'controller'    => 'services',
                                    'action'        => 'view',
                                    'route'         => 'default',
                                    ),
                                ),
                            )
                        );

            $this->bootstrap('layout');
            $layout = $this->getResource('layout');
            $view = $layout->getView();

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



在我的视图脚本布局中,我把它放在代码下面

echo $this->navigation()->menu()
                        ->setUlId('menu');


电流输出

<ul class="navigation" id="menu">
<li class="active">
    <a href="/test/public/admin/services">Services</a>
    <ul>
        <li>
            <a href="/test/public/admin/services/manage">Add Services</a>
        </li>
        <li class="active">
            <a href="/test/public/admin/services/view">View Services</a>
        </li>
    </ul>
</li>


预期产出

<ul class="navigation" id="menu">
<li>
    <a href="/test/public/admin/services">Services</a>
    <ul style='display:block;'>
        <li>
            <a href="/test/public/admin/services/manage">Add Services</a>
        </li>
        <li>
            <a class='selected' href="/test/public/admin/services/view">View Services</a>
        </li>
    </ul>
</li>

1 个答案:

答案 0 :(得分:1)

你必须在bootstrap中的'_initNavigation()'方法的末尾设置对象'view'的注册表。

Zend_Registry::set("view", $view);

创建插件'TestNavigation',在其中添加以下代码

 public function routeShutdown(Zend_Controller_Request_Abstract $request)
{

    foreach (Zend_Registry::get('view')->navigation()->getPages() as $page) {

            foreach ($page->getPages() as $subpage){                    
                $uri = $subpage->getHref();
                if ($uri === $request->getRequestUri()) {
                    $subpage->setClass('selected');
                }
            }
    }
}

就是这样,你已经完成了。