Issues

ZF-8611: Zend_navigation should support different containers

Issue Type: Improvement Created: 2009-12-22T05:33:32.000+0000 Last Updated: 2011-04-06T09:09:20.000+0000 Status: Closed Fix version(s): Reporter: Hans Müller (anno) Assignee: Kai Uwe (kaiuwe) Tags: - Zend_Navigation

  • View_Helper

Related issues: Attachments:

Description

In my case I want create a head navigation and main navigation with Zend_Navigaiton with plugins. The head and main naviagtion is created my themselves. So they have no connection with each other. So it would be good that Zend_Navigation supports more than one container because I also want use all the functionality of this component.

Here is some example code how I want create these navigations. Here I load the data from a model in the background.

head navigation:

<pre class="literal">
<?php

    class Plugin_NavigationHead extends Zend_Controller_Plugin_Abstract {

        public function preDispatch(Zend_Controller_Request_Abstract $request){

            $container = new Zend_Navigation();

$view = Zend_Layout::getMvcInstance()->getView();            

            $container->addPage(array("label" => "MainMenu",
                                         "module" => "default",
                                           "controller" => "index",
                                         "action" => "index"));

            $container->addPage(array("label" => "Submenu",
                                               "module" => "default",
                                            "controller" => "index",
                                          "action" => "index"));

            $container->addPage(array("label" => "NextSubMenu",
                                               "module" => "default",
                                            "controller" => "index",
                                          "action" => "index"));

            $view->navigation_head = $view->navigation($container)
                                     ->breadcrumbs()
                                  ->setSeparator(" -> ")->render();

    }
    }

?> 

main navigation:

<pre class="literal">
<?php

    class Plugin_NavigationMain extends Zend_Controller_Plugin_Abstract {

        public function preDispatch(Zend_Controller_Request_Abstract $request){

$view = Zend_Layout::getMvcInstance()->getView();

            $container = new Zend_Navigation();
                        
            $container->addPage(array("label" => "Main-Menu",
                                        "module" => "default",
                                         "controller" => "index",
                                     "action" => "index"));

            $container->addPage(array("label" => "Main-Submenu",
                                        "module" => "default",
                                     "controller" => "index",
                                        "action" => "index"));

            $view->navigation_head = $view->navigation($container)->menu()->render();

    }
    }

?> 

in my layout file I try to view these navigation but like I wrote above only the main menu is rendered.

layout.phtml file:

<pre class="literal">
<?= $this->navigation_main; ?>
<?= $this->navigation_head; ?>

But the problem is that only the main menu is rendered but not the breadcrumbs navigation. This support of different containers should only be activated if the navigation()-method was set more than one time. For the other case everything could be like now.

Comments

Posted by Kai Uwe (kaiuwe) on 2011-01-26T02:03:43.000+0000

Hi Hans, I can not reproduce your problem.

<pre class="highlight">
// Navigation
$containerOne = new Zend_Navigation(array(
    array(
        'label' => 'Home',
        'uri'   => 'home',
    ),
    array(
        'label' => 'Contact',
        'uri'   => 'contact',
    ),
    array(
        'label' => 'Imprint',
        'uri'   => 'imprint',
    ),
));

$containerTwo = new Zend_Navigation(array(
    array(
        'label' => 'Products',
        'uri'   => 'products',
        'pages' => array(
            array(
                'label'  => 'DVD',
                'uri'    => 'dvd',
                'active' => true,
            ),
            array(
                'label' => 'Games',
                'uri'   => 'games',
            ),
        ),
    ),
));

// View
$view = new Zend_View();

// Render
echo $view->navigation($containerOne)->menu();
echo $view->navigation($containerTwo)->breadcrumbs();



<pre class="highlight">


        <a href="home">Home</a>
    
<a href="products">Products</a> > DVD

You need a parent and an activated page. Or you can use the "minDepth":

<pre class="highlight">
$containerTwo = new Zend_Navigation(array(
    array(
        'label' => 'Products',
        'uri'   => 'products',
        'active' => true,
        'pages' => array(
            array(
                'label' => 'DVD',
                'uri'   => 'dvd',
            ),
            array(
                'label' => 'Games',
                'uri'   => 'games',
            ),
        ),
    ),
));

echo $view->navigation($containerTwo)->breadcrumbs()->setMinDepth(0);



<pre class="highlight">
Products

If you want to use the same helper with different containers, then the following must be respected:

You must set the container not for the proxy helper "Navigation". Set the container to the concrete helper (e.g. "Menu", "Breadcrumbs", ...).

<pre class="highlight">
// Render
echo $view->navigation()->menu($containerOne);
echo $view->navigation()->menu($containerTwo);



<pre class="highlight">
ul class="navigation">
    
        <a href="home">Home</a>
    
    
        <a href="contact">Contact</a>
    
    

        <a href="imprint">Imprint</a>
    




        <a href="products">Products</a>

                <a href="dvd">DVD</a>

            
            
                <a href="games">Games</a>
            
        

During loading a helper the container is set to the new concrete navigation helper (e.g. "Menu", "Breadcrumbs", ...). But when the helper is loaded, then no new container is set/inject.

<pre class="highlight">
public function findHelper($proxy, $strict = true)
{
    if (isset($this->_helpers[$proxy])) {
        return $this->_helpers[$proxy];
    }

    // ...

    $this->_inject($helper);
    $this->_helpers[$proxy] = $helper;

    return $helper;
}

protected function _inject(Zend_View_Helper_Navigation_Helper $helper)
{
    if ($this->getInjectContainer() && !$helper->hasContainer()) {
        $helper->setContainer($this->getContainer());
    }

    // ...
}

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts