title: ZF2: How to get Zend\Navigation inside custom route?
body: <p>I have custom router and I have to get access to <code>Zend\Navigation</code> inside this custom router. I was googling, asking and searching and no results :/</p>
  
  <p>All I need is to find nodes with 'link' param using Zend\Navigation in my Alias::match function.</p>
  
  <p>Here is my module.config.php:</p>
  
  <pre><code>'navigation' =&gt; array(
          'default' =&gt; array(
              'account' =&gt; array(
                  'label' =&gt; 'Account',
                  'route' =&gt; 'node',
                  'pages' =&gt; array(
                      'home' =&gt; array(
                          'label' =&gt; 'Dashboard',
                          'route' =&gt; 'node',
                          'params' =&gt; array(
                                      'id' =&gt; '1',
                                      'link' =&gt; '/about/gallery'
                                      ),
                      ),
                  ),
              ),
          ),
      ),
  [...]
  </code></pre>
  
  <p>And here is my Alias class:</p>
  
  <pre><code>// file within ModuleName/src/ModuleName/Router/Alias.php
  namespace Application\Router;
  
  use Traversable;
  use Zend\Mvc\Router\Exception;
  use Zend\Stdlib\ArrayUtils;
  use Zend\Stdlib\RequestInterface as Request;
  use Zend\Mvc\Router\Http;
  use Zend\ServiceManager\ServiceLocatorAwareInterface;
  use Zend\ServiceManager\ServiceLocatorInterface;
  
  class Alias extends Http\Segment implements ServiceLocatorAwareInterface
  {
  
      public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
      {
          $this-&gt;serviceLocator = $serviceLocator;
          return $this;
      }
  
      public function getServiceLocator()
      {
          return $this-&gt;serviceLocator;
      }
  
      public function match(Request $request, $pathOffset = null)
      {
          [...]
  
          return parent::match($request, $pathOffset);        
      }
  
  }
  </code></pre>
  
  <p><strong>EDITED:</strong></p>
  
  <p><strike>Now i know that I should inject service manager into my custom router. Let me know if You know how to do this :)</strike></p>
  
  <p><strong>EDITED:</strong></p>
  
  <p><strike>Ok, its not custom router but route. My bad. I was talking on <code>#zftalk</code> irc chanell and <code>AliasSegment</code> class should implements <code>ServiceLocatorAwareInterface</code>. Ok I've tried it but now there is another problem. </p>
  
  <p>In <code>setServiceLocator</code> function i can't get <code>service locator</code>. It returns null object, however <code>$serviceLocator</code> is class <code>Zend\Mvc\Router\RoutePluginManager</code>.</p>
  
  <pre><code>public function setServiceLocator(ServiceLocatorInterface $serviceLocator){
      $sl = $serviceLocator-&gt;getServiceLocator();
      var_dump($sl); // NULL
  }
  </code></pre>
  
  <p>Any ideas how to get Zend navigation from it ?</strike></p>
  
  <p><strong>EDITED</strong></p>
  
  <p>Corresponding to what @mmmshuddup said, I've changed my custom router class. (New version is above). Also in my <code>Module.php</code>, within <code>onBootstrap</code> function, I added this line:</p>
  
  <pre><code>$sm-&gt;setFactory('Navigation', 'Zend\Navigation\Service\DefaultNavigationFactory', true);
  </code></pre>
  
  <p>Navigation works and its instantiated before <code>route</code> so it should be visible within my Alias class but it's not.</p>
  
  <p>I've put into my <code>match</code> function in <code>Alias</code> class this line:</p>
  
  <pre><code>$servicesArray = $this-&gt;getServiceLocator()-&gt;getRegisteredServices();
  </code></pre>
  
  <p>and <code>$servicesArray</code> is almost empty. There is no service, no factories. The same line inserted into <code>onBootstrap</code>, just after setting new factory (as above) returns array with <code>navigation</code> and other services.</p>
  
  <p>The question is: how can i share this array (or ServiceManager) with my custom router: <code>Alias</code> ?</p>
  
  <p>I have to say that all I want to do was possible in ZF1 and it was quite easy.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I found a solution. The answer is below</p>