Zend Framework 2: How to place a redirect into a m

2019-02-02 14:33发布

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.

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.

Thanks in advance

4条回答
成全新的幸福
2楼-- · 2019-02-02 14:36

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;
        }
    }
}
?>
查看更多
虎瘦雄心在
3楼-- · 2019-02-02 14:37

Can you try this.

$front = Zend_Controller_Front::getInstance();
$response = new Zend_Controller_Response_Http();
$response->setRedirect('/profile');
$front->setResponse($response);
查看更多
家丑人穷心不美
4楼-- · 2019-02-02 14:38

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');        
        }
}
查看更多
甜甜的少女心
5楼-- · 2019-02-02 15:02

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.

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.

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);
}
查看更多
登录 后发表回答