title: Zf2 : Controller forwarding/calling generate 404 Error
body: <p>I am implementing separate auth solution for two different modules described in another question by me.</p>
  
  <p><a href="https://stackoverflow.com/questions/29146916/zend-framework-2-add-different-authentication-adapter-for-two-different-module/29241777">Zend framework 2 : Add different authentication adapter for two different modules</a>  </p>
  
  <p>Now in AuthListener file I write code for forward/call to an different controller/action if authentication failed. That is  </p>
  
  <pre><code>    $result = $this-&gt;adapter-&gt;authenticate();
  
      if (!$result-&gt;isValid()) {
  
          $response = $event-&gt;getResponse();
  
          // Set some response content
          $response-&gt;setStatusCode(401);
  
          $routeMatch = $event-&gt;getRouteMatch();
          $routeMatch-&gt;setParam('controller', 'First\Controller\Error');
          $routeMatch-&gt;setParam('action', 'Auth');
      }
  </code></pre>
  
  <p>Now I am getting 404 error - "The requested controller was unable to dispatch the request". First I think I do not added route for Error/Auth, but then I verified it got 404 for all other controller/action too. All are directly accessible through their respective route. But forwarding resulting in 404 error.  One important thing - I sending authentication request through phpunit to make unit test cases.</p>
  
  <p>UPDATE : route details : </p>
  
  <pre><code>'routes' =&gt; array(
      'rest' =&gt; array(
          'type' =&gt; 'Zend\Mvc\Router\Http\Segment',
          'options' =&gt; array(
              'route'    =&gt; '/rest[/:id]',
              'constraints' =&gt; array(
                  'id'     =&gt; '[0-9]+',
              ),
              'defaults' =&gt; array(
                  'controller' =&gt; 'First\Controller\Index'
              ),
          ),
      ),
      'error' =&gt; array(
          'type'    =&gt; 'segment',
          'options' =&gt; array(
              'route'    =&gt; '/rest-error/[/:action][/:id]',
              'constraints' =&gt; array(
                  'action' =&gt; '[a-zA-Z][a-zA-Z0-9_-]*',
                  'id'     =&gt; '[0-9]+',
              ),
              'defaults' =&gt; array(
                  'controller' =&gt; 'First\Controller\Error',
                  'action'     =&gt; 'auth',
              ),
          ),
      )
  ),
  
  'controllers' =&gt; array(
      'invokables' =&gt; array(
          'First\Controller\Auth' =&gt; 'First\Controller\AuthController',
          'First\Controller\Error' =&gt; 'First\Controller\ErrorController'
      ),
  ),
  </code></pre>
  
  <p>Module.php</p>
  
  <pre><code>$listener = $serviceManager-&gt;get('First\Service\AuthListener');
  $listener-&gt;setAdapter($serviceManager-&gt;get('Rest\Service\BasicAuthAdapter'));
  $eventManager-&gt;getSharedManager()-&gt;attach('First', 'dispatch', $listener, 100);
  </code></pre>
  
  <p>I also tried to use forward instead of above solutio, But that gives error for circular forward <code>Circular forwarding detected: greater than 10 nested forwards</code>. I think event called when forward called.</p>