我正在Zend Framework 2中的Module.php中尝试重定向。
我在网上得到了代码,它正在工作,但并不像预期的那样。
我正在尝试重定向到另一个模块。
这是代码。
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 ZendMvcApplication::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
?
我建议从控制器中获取ZF2重定向插件,并使用它重定向到另一个路由。您可以在StackOverflow的另一个问题中查看一个例子。