Zend_Navigation 将类添加到活动链接

发布于 2024-09-18 05:42:27 字数 405 浏览 5 评论 0原文

如何将类添加到活动导航链接?如果链接指向 URI /index/index 并且请求 URI 也是 /index/index,我希望该链接具有类,例如:

<li class="active">
    <a href="/index/index">Index</a>
</li>

这就是我在引导程序中初始化导航的方式:

protected function _initNavigation()
{
$navigation = new Zend_Navigation($this->getOption('navigation'));
$this->view->navigation($navigation);
}

How can I add a class to the active navigation link? If a link points to URI /index/index and the request URI is also /index/index, I would like the link to have class, for example:

<li class="active">
    <a href="/index/index">Index</a>
</li>

This is how I am initializing navigation in the bootstrap:

protected function _initNavigation()
{
$navigation = new Zend_Navigation($this->getOption('navigation'));
$this->view->navigation($navigation);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趴在窗边数星星i 2024-09-25 05:42:27

好的,

我已经通过编写控制器插件解决了这个问题:

<?php
class My_Controller_Plugin_PrepareNavigation extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer');
        $viewRenderer->initView();
        $view = $viewRenderer->view;

        $container = new Zend_Navigation(Zend_Registry::get('configuration')->navigation);
        foreach ($container->getPages() as $page) {
            $uri = $page->getHref();
            if ($uri === $request->getRequestUri()) {
                $page->setClass('active');
            }
        }
        $view->navigation($container);
    }
}

Ok,

I have solved this by writing a controller plugin:

<?php
class My_Controller_Plugin_PrepareNavigation extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer');
        $viewRenderer->initView();
        $view = $viewRenderer->view;

        $container = new Zend_Navigation(Zend_Registry::get('configuration')->navigation);
        foreach ($container->getPages() as $page) {
            $uri = $page->getHref();
            if ($uri === $request->getRequestUri()) {
                $page->setClass('active');
            }
        }
        $view->navigation($container);
    }
}
心意如水 2024-09-25 05:42:27

这是如何使用 Application 在 zend 框架的布局()中创建导航()。
好吧,至少有一种方法可以做到这一点。 CSS 类设置为

将其放入 Bootstrap.php 文件中:

protected function _initNavigation() 
{
     $this->bootstrap('layout');
     $layout = $this->getResource('layout');
     $view = $layout->getView();        
     include APPLICATION_PATH . '/layouts/scripts/menu.phtml';      
     $view->navigation($container);
}    

这允许您为文件 menu.phtml 中的菜单创建一个数组,以便您仍然可以在当前链接上维护活动类。由于某些奇怪的原因,如果您使用它,则必须在数组中包含控制器属性才能获取当前链接上的 CSS 活动类。

将类似的内容放入 /layouts/scripts/menu.phtml 文件中:

$container = new Zend_Navigation(array(
array(
    'label' => 'HOME',
    'id' => 'tasks',
    'uri'=>'/',
    'controller' => 'Index'
),
array(
    'label' => 'Contact',
    'uri' => 'contact',
    'controller' => 'Contact'
), 

.... more code here ...        

将其放入layout.phtml 文件中:

$options = array('ulClass' => 'menu');

This is how to create a navigation() in a layout() with zend frameworks using Application.
Well, at least one way of doing it. the CSS class is set on the

put this into the Bootstrap.php file:

protected function _initNavigation() 
{
     $this->bootstrap('layout');
     $layout = $this->getResource('layout');
     $view = $layout->getView();        
     include APPLICATION_PATH . '/layouts/scripts/menu.phtml';      
     $view->navigation($container);
}    

This allows you to create an array for a menu in the file menu.phtml, so that you can still maintain the active class on the current link. For some strange reason, if you use this you must include the controller property in the array to get the CSS active class on the current link.

put something like this into the /layouts/scripts/menu.phtml file:

$container = new Zend_Navigation(array(
array(
    'label' => 'HOME',
    'id' => 'tasks',
    'uri'=>'/',
    'controller' => 'Index'
),
array(
    'label' => 'Contact',
    'uri' => 'contact',
    'controller' => 'Contact'
), 

.... more code here ...        

put this into the layout.phtml file:

$options = array('ulClass' => 'menu');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文