How to pass parameters using cakePHP's redirect?

1

I'm 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 duplicate function, but does not pass the data as a parameter. According to the cakePHP documentation it should work.

How can I fix this?

    
asked by anonymous 13.01.2015 / 18:18

1 answer

2

Assuming your array $contact looks something like this:

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

You would have two ways to receive these values in your action . The first one is passing each of these key-value pairs as its own parameter in the array method of 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 get as follows:

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

As query string , you can pass the whole array :

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

It will look like this:

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