Closed andreidiaconescu closed 7 years ago
@andreidiaconescu Can you provide a short code example to reproduce the problem? (Like this one.)
Thanks!
The piece of code which may have a problem is: Zend\Navigation\AbstractContainer::hasChildren()
public function hasChildren() { return $this->valid() && $this->current()->hasPages(); }
The method current() returns:
return $this->pages[$hash];
So $this->current()->hasPages() returns true if current child of current page has pages;
Providing a code example is a bit harder, as the app is a bit large, and for navigation, it has a factory, navigation template, another the template where helper is used show the menu, etc. But if really necessary, i will give a sample of code as is used in the app.
Is what i wrote enough or you need a code example ?
code sample:
template where navigation view helper is used:
$this->navigation('admin_navigation')->menu()->setPartial(array('partials/admin-menu', ''));
echo $this->navigation('admin_navigation')->menu();
pages configuration array:
<?php
return array(
// All navigation-related configuration is collected in the 'navigation' key
'navigation' => array(
'admin' => array(
'list-texts' => array(
'label' => 'Text blocks',
'route' => 'backend',
'icon-name' => 'fa fa-file-text-o',
'params' => array('controller' => 'text', 'action' => 'index'),
),
'sys' => array(
'label' => 'System',
'route' => 'backend',
'icon-name' => 'fa fa-cogs',
'params' => array('controller' => 'user', 'action' => 'index'),
'pages' => array(
'users' => array(
'label' => 'Users',
'route' => 'backend',
'icon-name' => 'fa fa-user',
'params' => array('controller' => 'user', 'action' => 'index'),
),
'list-labels' => array(
'label' => 'Labels',
'route' => 'backend',
'icon-name' => 'fa fa-book',
'params' => array('controller' => 'label', 'action' => 'index'),
),
)
),
),
),
);
3. partial for the navigation
<?php $i = 0; foreach ($this->container as $page) { $i++; ?>
<?php } ?>
Here you can find an old related issue: https://github.com/zendframework/zendframework/issues/4517
And a working example:
<?php
/**
* @var \Zend\View\Renderer\PhpRenderer $this
* @var \Zend\Navigation\Navigation $container
*/
?>
<ul>
<?php foreach ($container as $page): ?>
<li>
<?= $page->getLabel() ?>
<?php if ($page->hasPages()): ?>
<ul class="sub-menu">
<?php foreach ($page as $child): ?>
<li>
<?= $child->getLabel() ?>
</li>
<?php endforeach ?>
</ul>
<?php endif ?>
</li>
<?php endforeach ?>
</ul>
Btw. you can simplify this:
$this->navigation('admin_navigation')->menu()->setPartial(array('partials/admin-menu', ''));
echo $this->navigation('admin_navigation')->menu();
with:
<?= $this->navigation('admin_navigation')->menu()->setPartial('partials/admin-menu') ?>
thanks a lot !
Hello,
After trying for a few hours to make the zend navigation work, when upgrading from ZF2(2.4) to ZF3, i think i have found a problem with method: Zend\Navigation\AbstractContainer->hasChildren() In the past it used to work like this (in ZF 2.4):
Is this a problem or not ?
Thank you !