2013-03-08 101 views
9

如何重定向到另一个模块?Zend Framework 2重定向

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

这似乎不工作,有什么想法?

+2

如果您要重定向,重定向到路线名称。如果您只想分派另一个操作,请使用forward()助手。请参阅http://framework.zend.com/manual/2.1/en/modules/zend.mvc.plugins.html – Sam 2013-03-08 22:04:40

回答

24

这是我从我的控制器重定向到另一个途径:

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

当DNS搜索是我要重新导向和companyid路线是网址参数。

在URL变为/ DNS /搜索/ 1(例如)

+0

并确保您不会错过“return”关键字,因为如果您期望执行代码,可能会发生坏事在没有它的地方停下来;-) – AlexMA 2014-09-19 16:19:58

6

这是重定向在ZF2的方式结束:

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

我希望这可以帮助你。

+3

'ModuleName'应该是'RouteName' – 2014-07-25 08:07:10

16

其中一个简单的方法来重定向是:

return $this->redirect()->toUrl('YOUR_URL'); 
+1

BEST BEST BEST最好的例子,这工作,这是最好的。 'return $ this-> redirect() - > toUrl('/ make-world-easy?id = thank-you');' – YumYumYum 2016-10-26 23:39:40

+0

这个解决方案是为了不同的目的:它用于链接到其他资源。原则上,这并不重要,但如果您需要Zend-ish方法,则接受的答案是最好的。 – 2017-01-28 08:14:59

1

谷歌认为,这个问题是有关ZF2锚重定向,所以我会在这里添加的答案,如果你不介意。我们可以只使用fragment选项的toRoute第三个参数:

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

我的路线:

'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', 
     ), 
    ), 
), 

因此,用户将被引导到SMTH像/chats/view/12/#m134