2014-09-06 51 views
1

我想在Module.php重定向Zend框架2.Zend框架2重定向在onBootStrap與網站網址

我得到的代碼在網上,它是工作,但並不如預期。

我正試圖重定向到一個不同的模塊。

這是代碼。

public function onBootStrap($e){ 
    $container = new Container(); 
    if(!$container || !$container->admin_id){ 
     // Assuming your login route has a name 'login', this will do the assembly 
     // (you can also use directly $url=/path/to/login) 
     $url = $e->getRouter()->assemble(array('action' => 'login'), array('name' => 'admin')); 
     $response=$e->getResponse(); 
     $response->getHeaders()->addHeaderLine('Location', $url); 
     $response->setStatusCode(302); 
     $response->sendHeaders(); 
     // When an MvcEvent Listener returns a Response object, 
     // It automatically short-circuit the Application running 
     // -> true only for Route Event propagation see Zend\Mvc\Application::run 

     // To avoid additional processing 
     // we can attach a listener for Event Route with a high priority 
     $stopCallBack = function($event) use ($response){ 
      $event->stopPropagation(); 
      return $response; 
     }; 
     //Attach the "break" as a listener with a high priority 
     $e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_ROUTE, $stopCallBack,-10000); 
     return $response; 
    } 
} 

它的工作原理和重定向我http://localhost/admin/login 但我的實際網站的網址是http://localhost/MantissaAdmin/public/ 所以應該重定向我http://localhost/MantissaAdmin/public/admin/login

我怎樣才能重定向到http://localhost/MantissaAdmin/public/admin/login

+1

您是否嘗試過將'force_canonical'=> true傳遞給options參數。如果有的話,你的.htacces中也可能需要RewriteBase。 – 2014-09-06 21:58:46

回答