Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 Redirect

How can I redirect to another module?

return $this->redirect()->toRoute(null, array(
    'module'     => 'othermodule',
    'controller' => 'somecontroller',
    'action'     => 'someaction'
));

This doesn't seem to work, any ideas?

like image 966
Георги Банков Avatar asked Mar 08 '13 20:03

Георги Банков


4 Answers

This is how I redirect from my Controller to another Route:

return $this->redirect()->toRoute('dns-search', array(
    'companyid' => $this->params()->fromRoute('companyid')
));

Where dns-search is the route I want to redirect to and companyid are the url params.

In the end the URL becomes /dns/search/1 (for example)

like image 197
Diemuzi Avatar answered Nov 02 '22 23:11

Diemuzi


One of the easy ways to redirect is:

return $this->redirect()->toUrl('YOUR_URL');
like image 33
Manish Avatar answered Nov 02 '22 22:11

Manish


This is the way of redirection in ZF2:

return $this->redirect()->toRoute('ModuleName',
  array('controller'=>$controllerName,
        'action' => $actionName,
        'params' =>$params));

I hope this helps you.

like image 6
KumarA Avatar answered Nov 03 '22 00:11

KumarA


Google thinks, that this question is relevant to zf2 redirect with anchor, so I will add the answer here, if you don't mind. We can just use fragment option in third argument of toRoute:

public function doAction(){
    return $this->redirect()
         ->toRoute('chats', 
                   array('action' => 'view', 
                       'id' => $message->getChat()->getId()
                   ),
                   array('fragment' => 'm' . $message->getId())
    );
}

My route:

'chats' => array(
    'type' => 'segment',
    'options' => array(
        'route' => '/chats/[:action/][:id/]',
        'constraints' => array(
            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
            'id' => '[0-9]+',
        ),
        'defaults' => array(
            'controller' => 'Application\Controller\Chats',
            'action' => 'index',
        ),
    ),
),

So, user will be directed to smth like /chats/view/12/#m134

like image 1
shukshin.ivan Avatar answered Nov 02 '22 23:11

shukshin.ivan