[英]Difference between init() and onBootStrap() in Zend Framework 2?
[英]Zend Framework 2 Redirect in onBootStrap with site URL
我正在嘗試在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 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
但是我的實際站點URL是http://localhost/MantissaAdmin/public/
因此應該將我重定向到http://localhost/MantissaAdmin/public/admin/login
如何重定向到http://localhost/MantissaAdmin/public/admin/login
?
我建議從控制器中獲取ZF2重定向插件 ,並將其用於重定向到其他路由。 您可以在StackOverflow的另一個問題中查看示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.