如何在Zend Framework中使用重定向助手传递参数?

9

在Zend Framework中,使用redirectory helper能否传递参数 ($_POST或$_GET)?以下代码将重定向到当前控制器的index操作,但我也想传递一些参数。

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

Zend文档中没有提到这个。
2个回答

19
当然可以。这段代码示例来自于Action Helpers文档(请看页面下方约2/3处的Redirector部分)。你可能需要获取重定向助手的引用,并像这段代码一样调用其中一个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'));
    }
}

@Andy Shellam // 啊!我怎么会错过那个!非常感谢你! - Moon
上面提到的Action Helpers文档返回了404错误,请尝试使用1.12版本的文档 - https://framework.zend.com/manual/1.12/en/zend.controller.actionhelpers.html - PiggyMacPigPig
我使用Zend 3.0.0,我的参数顺序是redirector(module, action, controller, params)。 - Nam G VU

8

将数组作为第4个参数传递:

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

你使用的“module”参数是什么? - softwareplay
1
@softwareplay 您正在使用的模块名称,如果没有模块,我认为您应该将其设置为空。http://framework.zend.com/manual/2.0/en/user-guide/modules.html - ‌‌R‌‌‌.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接