How to pass parameters using cakephp redirect?

Asked

Viewed 1,288 times

1

I am using the following code snippet in my controller:

$this->redirect(array('action' => 'duplicate', $contact));

The $contact variable contains an array.

The command redirects to the function duplicate, but does not pass the data as a parameter. According to the cakephp documentation it should work.

How to fix this problem?

1 answer

2


Supposing that your array $contact be something like that:

array(
    'nome' => 'Fulano',
    'email' => '[email protected]'
);

You would have two ways to receive these values in your action. The first is passing each of these pairs of value key as a parameter in array of the method redirect:

$this->redirect(array(
    'action' => 'duplicate',
    'nome' => 'Fulano',
    'email' => '[email protected]')
);

All values subsequent to action will be a parameter of your method, which you will receive so:

// URL: controller/duplicate/nome:Fulano/email:[email protected]
public function duplicate($nome, $email) {
}

Or how query string, you can spend the whole array:

$this->redirect(array(
    'action' => 'duplicate',
    '?' => $contact)
);

That’s how you’ll stay:

// URL: controller/duplicate?nome=Fulano&[email protected]
public function duplicate() {
    echo $this->request->params['nome'];
    echo $this->request->params['email'];
}
  • 1

    Thank you very much for the reply, I did the first way and gave it right here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.