web-dev-qa-db-ja.com

Zend Framework 2リダイレクト

別のモジュールにリダイレクトするにはどうすればよいですか?

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

これはうまくいかないようです、何かアイデアはありますか?

これは私がコントローラーから別のルートにリダイレクトする方法です:

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

ここで、dns-searchはリダイレクト先のルートであり、companyidはURLパラメータです。

最終的に、URLは/ dns/search/1になります(例)

26
Diemuzi

リダイレクトする簡単な方法の1つは次のとおりです。

return $this->redirect()->toUrl('YOUR_URL');
17
Manish

これは、ZF2でのリダイレクトの方法です。

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

これがお役に立てば幸いです。

6
KumarA

Googleは、この質問はzf2 redirect with anchorに関連していると考えているので、よろしければここに回答を追加します。 fragmentの3番目の引数で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',
        ),
    ),
),

したがって、ユーザーは/chats/view/12/#m134のようなsmthに転送されます

1
shukshin.ivan