[英]Pass parameters with redirect without putting them in url in Zend Framework 2
有沒有辦法像ZF2中那樣通過重定向傳遞參數
return $this->redirect()->toRoute('account', array('test' => 'ok'));
如果可能的話,如何獲得test
參數?
PS。 test
不是映射到路由的參數。 account
是我的控制者,它具有默認操作,因此無需在redirect
指定操作。
我認為您可以執行以下操作:假設您的模塊是Myaccount,您的控制器是AccountController,您的操作是索引(我認為是您的默認操作)。 因此,在您的module.config.php中,您將具有以下內容:
return array(
'controllers' => array(
'invokables' => array(
'Account' => 'Myaccount\Controller\AccountController',
然后,在某些操作中,您可以使用ZF2樣式傳遞帶有重定向的參數,如下所示:
return $this->redirect()->toRoute('account', array(
'controller' => 'account',
'action' => 'index',
'test' => $testVal // in this case "okay"
));
哦,是的,您在module.config.php中的路由也很重要:
'router' => array(
'routes' => array(
'account' => array(
'type' => 'segment',
'options' => array(
'route' => '/account[/:action][/:test]',
因為這就是ZF2知道去向何處以及向其追加內容的方式,所以在我們的情況下,它會導致“ ../account/index/test/okay”,因為好的是我們對“ test”的價值。
希望能幫助到你 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.