I want to check for user id in the constructor of my ZF2 Controller and if it is not defined then want to redirect to any external URL or to other controller I have tried:
$this->redirect()->toRoute("campaign");
and
$this->redirect()->toUrl("http://example.com");
Both approaches give me the same error message:
Url plugin requires that controller event compose a router; none found
Service Locator is not present in the Controller's constructor, but you can override onDispatch method inherited from AbstractActionController.
Use it like this:
public function onDispatch(\Zend\Mvc\MvcEvent $e)
    {
        $session = new \Zend\Session\Container('base');
            if(!$session->offsetExists('id'))
            $this->redirect()->toRoute("campaign"); and $this->redirect()->toUrl("http://example.com")
        return parent::onDispatch($e);
    }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With