1


我正在使用 zend 导航来创建菜单。我想将默认类“活动”更改为“已选择”,并且我还想将该类添加到活动链接的锚标记。这是我在引导程序中放入的示例代码

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>

4

1 回答 1

1

您只需在引导程序中的“_initNavigation()”方法的末尾设置对象“视图”的注册表。

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');
                }
            }
    }
}

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

于 2013-01-08T10:58:10.863 回答