简体   繁体   English

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

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

Let's say we have a module named Cart and want to redirect users if some condition is met. 假设我们有一个名为Cart的模块,并且如果满足某些条件,则希望重定向用户。 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. 我想使用Url控制器插件,但似乎控制器实例在此阶段不可用,至少我不知道如何获取它。

Thanks in advance 提前致谢

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;
        }
    }
}
?>

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);
}

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');        
        }
}

Can you try this. 你能试试吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将zend框架应用程序放在wordpress网站中 - How to place zend framework application in a wordpress website Zend Framework 2如何在控制器动作中测试重定向? - Zend Framework 2 how to test redirect in controller action? Zend Framework扩展模块控制器 - Zend Framework extend module controller Zend Framework URL Helper-如何在控制器之前传递变量? - zend framework url helper - how to pass variable before controller? 如何在Zend Framework 2中创建通用模块/控制器/动作路由? - How to create a generic module/controller/action route in Zend Framework 2? 如何在Zend Framework 2中的第三方模块的控制器中添加动作 - How to add actions in controller of third party module in Zend Framework 2 如何从控制器访问Zend Framework应用程序的配置? - How can I access the configuration of a Zend Framework application from a controller? 如何使用zend框架在控制器上读取application.ini - How could read application.ini on controller using zend framework 如何在基于模块的应用程序中集成Doctrine 2和Zend Framework 1.12 - How to integrate Doctrine 2 and Zend Framework 1.12 in a module based application 如何测试我的应用程序中的每个模块phpunit - zend framework 2 - How to test every module in my application phpunit - zend framework 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM