Zend Framework 2使用站点URL在onBootStrap中重定向

时间:2014-09-06 19:39:21

标签: php redirect zend-framework2

我试图在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 但我的实际网站网址是http://localhost/MantissaAdmin/public/ 所以应该将我重定向到http://localhost/MantissaAdmin/public/admin/login

如何重定向到http://localhost/MantissaAdmin/public/admin/login

1 个答案:

答案 0 :(得分:2)

我建议从控制器中抓取the ZF2 redirect plugin并将其用于重定向到另一条路线。您可以查看示例here in another question on StackOverflow