2010-03-31 68 views
9

是否有可能在Zend Framework中使用redirectory helper傳遞參數($ _POST或$ _GET)?下面的代碼重定向到當前控制器的索引操作,但我想將一些參數傳遞給它。如何在Zend Framework中使用重定向助手傳遞參數?

$this->_helper->redirector("index"); 

Zend Documenataion對此沒有任何說明。

回答

19

當然。這是Action Helpers documentation中的代碼示例(請參閱Redirector部分,大約是頁面向下的2/3。)您可能需要獲取對重定向器助手的引用,並調用其中一個goto*方法,如此代碼所執行的操作。

class ForwardController extends Zend_Controller_Action 
{ 
    /** 
    * Redirector - defined for code completion 
    * 
    * @var Zend_Controller_Action_Helper_Redirector 
    */ 
    protected $_redirector = null; 

    public function init() 
    { 
     $this->_redirector = $this->_helper->getHelper('Redirector'); 
    } 

    public function myAction() 
    { 
     /* do some stuff */ 

     // Redirect to 'my-action' of 'my-controller' in the current 
     // module, using the params param1 => test and param2 => test2 
     $this->_redirector->gotoSimple('my-action', 
             'my-controller', 
             null, 
             array('param1' => 'test', 'param2' => 'test2')); 
    } 
} 
+0

@Andy Shellam修改//啊!我怎麼會錯過!萬分感謝! – Moon 2010-03-31 10:54:25

+0

上面引用的Action Helpers文檔返回的是404版本,而不是版本1.12 - https://framework.zend.com/manual/1.12/en/zend.controller.actionhelpers.html – PiggyMacPigPig 2016-10-03 10:44:07

+0

我使用Zend 3.0.0,並且我的參數順序是重定向器(模塊,動作,控制器,參數) – 2018-01-28 16:42:02

8

傳遞一個數組作爲第四個參數:

$this->_helper->redirector('action', 'controller', 'module', array('param1' => 'value1')); 
+0

什麼是您使用的「模塊」參數? – softwareplay 2014-03-03 15:55:31

+1

@softwareplay您正在使用的模塊的名稱,如果沒有模塊,我認爲您應該將其設置爲空。 http://framework.zend.com/manual/2.0/en/user-guide/modules.html – Rahman 2014-03-18 09:27:31