title: ZF2, create dynamic menu at module initiation
body: <p>I have a menu which I need to create dynamically (some blog pages are added from a database). <a href="https://stackoverflow.com/questions/12531423/zf2-dynamic-menu-with-zend-navigation-navigation">ZF2 Dynamic Menu with Zend\Navigation\Navigation</a> addresses how to do this for an individual controller/action.</p>
  
  <p>But how should this be done for all requests, at the moment of initialising the module?</p>
  
  <p>I need at least the routeMatch object (to get the language parameter) and I have seen the below to get that:</p>
  
  <pre><code>    public function onBootstrap(EventInterface $e) 
      { 
          $app = $e-&gt;getApplication(); 
          $em  = $app-&gt;getEventManager(); 
  
          $em-&gt;attach(MvcEvent::EVENT_ROUTE, function($e) { 
              $routeMatch = $e-&gt;getRouteMatch();
          }); 
      }
  </code></pre>
  
  <p>But in the docs it says:</p>
  
  <p>"the onBootstrap() method is called for every module implementing this feature, on every page request, and should only be used for performing lightweight tasks such as registering event listeners."</p>
  
  <p>What would be the best place and way to initialise dynamic navigation (or other more complicated logic) in Zend Framework 2?</p>