aladar
aladar

Reputation: 61

Zend Framework 2 (ZF2) forwarding to a different module controller

I have a controller that processes a form. If posted data has certain value, I need to forward the user to a controller residing in a different module. I've tried this:

$post = $this->params()->fromPost();
if (!isset($post['terms'])) {
    $this->forward()->dispatch('Job\Controller\IndexController', 
        array('action' => 'index')); 
}

But the above code doesn't work. The documentation states that the controller has to be ServiceLocatorAware, but I don't know how to make the controller servicelocatoraware. Any idea how I can accomplish this?

Here is the error message from xdebug.

/home/test/mydomain/vendor/ZF2/library/Zend/ServiceManager/ServiceManager.php:496

Message:

Zend\Mvc\Controller\ControllerManager::get was unable to fetch or create an instance for Job\Controller\IndexController

Stack trace:

Upvotes: 0

Views: 991

Answers (3)

asologor
asologor

Reputation: 707

You've got an error because in the ServiceLocator your controller alias is

'Job\Controller\Index'

so you should use this controller path instead of 'Job\Controller\IndexController'

Upvotes: 1

sheauren
sheauren

Reputation: 96

try use header to redirect.

// $url is new target
header('Location:'.$url,1);
exit(0);

that's my redirect method, myabe you can use it:

public function Redirect($url,$withCurrentQueryString=true){
    if($withCurrentQueryString==true){
        if(strpos($url,'?')>0){
        $url=$url.'&'.$_SERVER['QUERY_STRING'];
    }
    else{
        $url=$url.'?'.$_SERVER['QUERY_STRING'];
    }
    }
    header('Location:'.$url,1);
    exit(0);
}

Upvotes: 0

Paul_O
Paul_O

Reputation: 33

Use the ServiceLocator Aware Interface in your controller:

use Zend\ServiceManager\ServiceLocatorAwareInterface;

Upvotes: 0

Related Questions