Skip to content

Instantly share code, notes, and snippets.

@outrunthewolf
Created March 12, 2014 09:00
Show Gist options
  • Save outrunthewolf/9503301 to your computer and use it in GitHub Desktop.
Save outrunthewolf/9503301 to your computer and use it in GitHub Desktop.
Dynamic routing Zend2
public function onBootstrap( MvcEvent $e ) {
// Get the service and event managers
$events = $e->getApplication()->getEventManager();
// Attach a dynamic render event
$events->attach(MvcEvent::EVENT_RENDER, function(MvcEvent $event) {
// Service Manager
$sm = $event->getParam('application')->getServiceManager();
$mod = $sm->get('ModuleManager');
// Add in a special route
foreach($mod->getModules() as $module) {
// Define some routes
$controller = __NAMESPACE__ . "Controller\BaseController";
$routes = Segment::factory(array(
'route' => '/admin/test',
'defaults' => array(
'controller' => $controller,
'action' => 'index'
)
));
if($module == "MyModule")
{
$router = $sm->get('router');
$router->addRoute($module, $routes, 1000);
var_dump($router->hasRoute($module));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment