douguizhuang8276
2012-06-24 11:42
浏览 102
已采纳

CakePHP在Controller :: redirect中传递参数

In controller actions to make redirect I use this:

$this->redirect(array('controller' => 'tools', 'action' => 'index'));

or this

$this->redirect('/tools/index');

And when I pass data with redirect I use this:

$this->redirect('tools/index/?myArgument=12');

But I couldn't find how to pass "myargument" by "this-redirect-array" notation.
I don't want to use this because some routing issues:

$this->redirect(array('controller' => 'tools', 'action' => 'index', "myArgument"));

I need something like this:

$this->redirect(array('controller' => 'tools', 'action' => 'index', "?myArgument=12"));
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dounuo8797 2012-06-25 15:22
    已采纳

    Cake does indeed support query arguments using the question mark, like this:

    $this->redirect(array(
        'controller' => 'tools', 'action' => 'index', '?' => array(
            'myArgument' => 12
        )
    ));
    

    http://book.cakephp.org/2.0/en/development/routing.html#reverse-routing

    But it would be better to just do, like des said:

    $this->redirect(array(
        'controller' => 'tools', 'action' => 'index', 'myArgument' => 12
    ));
    
    点赞 打赏 评论
  • dongque4778 2012-06-24 11:48

    This should work:

    $this->redirect(array('controller' => 'tools', 'action' => 'index', 'myArgument' => 12));
    

    Take a look at CakePHP Cookbook - Controller::redirect

    Accessing request parameters:

    $this->request['myArgument'];
    $this->request->myArgument;
    $this->request->params['myArgument'];
    
    点赞 打赏 评论
  • dpb56083 2015-01-20 03:05

    Using this to redirect:

    $this->redirect(array('controller' => 'tools', 'action' => 'index', 'myArgument' => 12));
    

    And Router::connectNamed() to router.php to change separator from ":" to "=":

    Router::connectNamed(
        array('myArgument' => array('action' => 'index', 'controller' => 'tools')), array('default' => false, 'greedy' => false, 'separator' => '=')
    

    );

    点赞 打赏 评论

相关推荐 更多相似问题