php Zend Framework 2:如何在应用程序到达控制器之前将重定向放入模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14169699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:47:31  来源:igfitidea点击:

Zend Framework 2: How to place a redirect into a module, before the application reaches a controller

phpredirectzend-framework2

提问by Artyom Tokarev

Let's say we have a module named Cart and want to redirect users if some condition is met. I want to place a redirect at the module bootstrapping stage, before the application reaches any controller.

假设我们有一个名为 Cart 的模块,并且希望在满足某些条件时重定向用户。我想在应用程序到达任何控制器之前在模块引导阶段放置一个重定向。

So here is the module code:

所以这是模块代码:

<?php
namespace Cart;

class Module
{
    function onBootstrap() {
        if (somethingIsTrue()) {
            // redirect
        }
    }
}
?>

I wanted to use the Url controller plugin, but it seems the controller instance is not available at this stage, at least I don't know how to get it.

我想使用Url控制器插件,但现在似乎控制器实例不可用,至少我不知道如何获取它。

Thanks in advance

提前致谢

回答by yechabbi

This should do the necessary work:

这应该做必要的工作:

<?php
namespace Cart;

use Zend\Mvc\MvcEvent;

class Module
{
    function onBootstrap(MvcEvent $e) {
        if (somethingIsTrue()) {
            //  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(), array('name' => 'login'));
            $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;
        }
    }
}
?>

回答by Nikitas

Of course it gives you an error because you must attach your listener to an event. In the folllowing example i use SharedManager and i attach the listener to AbstractActionController.

当然它会给你一个错误,因为你必须将你的监听器附加到一个事件上。在以下示例中,我使用 SharedManager 并将侦听器附加到AbstractActionController.

Of course you can attach your listener to another event. Below is just a working example to show you how it works. For mor info visit http://framework.zend.com/manual/2.1/en/modules/zend.event-manager.event-manager.html.

当然,您可以将侦听器附加到另一个事件。下面只是一个工作示例,向您展示它是如何工作的。有关更多信息,请访问http://framework.zend.com/manual/2.1/en/modules/zend.event-manager.event-manager.html

public function onBootstrap($e)
{
    $e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
        $controller = $e->getTarget();
        if (something.....) {
            $controller->plugin('redirect')->toRoute('yourroute');
        }
    }, 100);
}

回答by Akansh Braj

The page isn't redirecting properly on error

页面未正确重定向错误

public function onBootstrap($e) {

        $e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
        if(someCondition==true) {
           $controller->plugin('redirect')->toRoute('myroute');        
        }
}

回答by Nandakumar V

Can you try this.

你可以试试这个。

$front = Zend_Controller_Front::getInstance();
$response = new Zend_Controller_Response_Http();
$response->setRedirect('/profile');
$front->setResponse($response);