Zend Framework 2 Redirect

后端 未结 4 2482
鱼传尺愫
鱼传尺愫 2021-02-20 17:26

How can I redirect to another module?

return $this->redirect()->toRoute(null, array(
    \'module\'     => \'othermodule\',
    \'controller\' => \'s         


        
相关标签:
4条回答
  • 2021-02-20 17:36

    One of the easy ways to redirect is:

    return $this->redirect()->toUrl('YOUR_URL');
    
    0 讨论(0)
  • 2021-02-20 17:42

    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.

    0 讨论(0)
  • 2021-02-20 17:45

    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)

    0 讨论(0)
  • 2021-02-20 17:49

    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

    0 讨论(0)
提交回复
热议问题