Skip to content

Instantly share code, notes, and snippets.

@devmatheus
Last active December 21, 2015 12:29
Show Gist options
  • Select an option

  • Save devmatheus/6306438 to your computer and use it in GitHub Desktop.

Select an option

Save devmatheus/6306438 to your computer and use it in GitHub Desktop.
Navigation Twitter Boostrap + ZF2 com sub-pages. Resultado final: http://i.imgur.com/Op3DGmk.png https://gist.github.com/devmatheus/4689656
<?php
return array(
'navigation' => array(
'default' => array('conteudo' => array('label' => 'Conteúdo',
'route' => 'home-admin',
'pages' => $arrayModulos),
$arrayModulos['usuarios'])
),
);
<ul class='nav'>
<?php
function menu($sub) {
foreach ($sub as $pages):
$active = $pages->isActive() ? ' class="active"' : '';
if (count($pages->getPages()) > 0): // se tem filho printa o ul (agrupador de links)
$return .= '<li class="dropdown-submenu"' . $active . '>
<a tabindex="-1" href="#">' . $pages->getLabel() . '</a>
<ul class="dropdown-menu">';
$return .= menu($pages->getPages()); // e então chamo a função novamente para printar os filhos, e se esses filhos tiverem filhos, irão chamar filhos dos filhos, assim por diante hahahaha
$return .= '</ul></li>';
else: // se não for ter filho então printa os links
$action = null;
if($pages->getAction())
$action = '/' . $pages->getAction(); // se não tem action não coloca a barra (/)
$return .= '<li' . $active . '><a href="/admin/' . str_replace('admin-', '', $pages->getRoute()) . $action . '">' . $pages->getlabel() . '</a></li>';
// não consegui usar o helper url aqui, devido estar usando esta function, então tive de passar a url desta forma :(
endif;
endforeach;
return $return;
}
foreach ($this->container as $page):
$sub = $page->getPages();
if ($page->isActive())
$liClasses[] = 'active';
if (count($sub) > 0):
$liClasses[] = 'dropdown';
$aAttribs = ' class="dropdown-toggle" data-toggle="dropdown"';
endif;
if (count($liClasses) > 0)
$liClass = ' class="' . implode(' ', $liClasses) . '"';
echo '<li' . $liClass . '><a href="' . $this->url($page->getRoute()) . '"' . $aAttribs . '>' . $page->getlabel() . '</a>'; // pais
if (count($sub) > 0) // se não for printar os filhos, ou seja, se for a primeira vez que estiver printando o controller no menu
echo '<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">' . menu($sub) . '</ul>';
unset($aAttribs, $liClass, $liClasses);
endforeach;
?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment