Skip to content

Instantly share code, notes, and snippets.

Created February 25, 2013 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5034200 to your computer and use it in GitHub Desktop.
Save anonymous/5034200 to your computer and use it in GitHub Desktop.
Dynamic Navigation
//Module.php
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Application\View\Helper\FlashMessenger;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$e->getApplication()->getServiceManager()->get('translator');
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
public function getServiceConfig()
{
return array(
'factories' => array(
'Navigation' => 'Application\Navigation\MyNavigationFactory'
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
//other code
}
//Navigation/MyNavigation
namespace Application\Navigation;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Navigation\Service\DefaultNavigationFactory;
class MyNavigation extends DefaultNavigationFactory
{
protected function getPages(ServiceLocatorInterface $serviceLocator)
{
if (null === $this->pages) {
//FETCH data from table menu :
$fetchMenu = $serviceLocator->get('menu')->fetchAll();
$configuration['navigation'][$this->getName()] = array();
foreach($fetchMenu as $key=>$row)
{
$configuration['navigation'][$this->getName()][$row['name']] = array(
'label' => $row['label'],
'route' => $row['route'],
);
}
if (!isset($configuration['navigation'])) {
throw new Exception\InvalidArgumentException('Could not find navigation configuration key');
}
if (!isset($configuration['navigation'][$this->getName()])) {
throw new Exception\InvalidArgumentException(sprintf(
'Failed to find a navigation container by the name "%s"',
$this->getName()
));
}
$application = $serviceLocator->get('Application');
$routeMatch = $application->getMvcEvent()->getRouteMatch();
$router = $application->getMvcEvent()->getRouter();
$pages = $this->getPagesFromConfig($configuration['navigation'][$this->getName()]);
$this->pages = $this->injectComponents($pages, $routeMatch, $router);
}
return $this->pages;
}
}
//Navigation/DefaultNavigationFactory
namespace Application\Navigation;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class MyNavigationFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
var_dump("test");
$navigation = new MyNavigation();
return $navigation->createService($serviceLocator);
}
}
//layout.phtml
<?php echo $this->navigation('Navigation')->menu()->setPartial(array('menu.phtml','default')); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment