Passare parametri tramite reindirizzamento
-
12-11-2019 - |
Domanda
È possibile passare i parametri tramite reindirizzamento?Ho provato un sacco di opzioni, ma nulla sembra funzionare.Il mio ultimo approccio è:
return $this->redirect(array('Users::helloworld', 'args' => array('id' => 'myId')));
.
Allora ho creato un percorso:
Router::connect('/Users/helloworld?id={:id}', array('controller' => 'Users', 'action' => 'helloworld'));
.
Ma tutto quello che ottengo è users/helloworld/myId
Soluzione
args
fa parte dei percorsi e verrà convertito in un URL usando il percorso molto generico (non quello che hai creato e non richiedere)
Per ottenere una stringa di query , usa semplicemente il tasto ?
:
.
return $this->redirect(array(
'Users::helloworld',
'?' => array('id' => $myId)
));
// will use the route:
// /{:controller}/{:action}/{:args}
// and generate
// /users/helloworld?id=$myId
Il test per questo: https://github.com/unionofrad/lithium/blob/master/tests/cases/net/http/routetest.php#l374-405
Altri suggerimenti
Invece di definire un percorso separato per passare gli argomenti, potresti semplicemente fare quanto segue.
Diciamo che si desidera passare 2 argomenti: $ arg1 & $ arg2 al helloworld azione dei tuoi utenti controller:
.return $this->redirect(array(
'Users::helloworld',
'args' => array(
$arg1,
$arg2
)
));