6
I need to pass an object to a page. For example: I wish that the moment I redirect to a particular page that will present html, that in it I could recover this object and use whatever is in it.
Someone would have an example of how to do this?
This is an example I made to try to explain better:
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/exemplomvc/src/model/to/CadastroTO.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/exemplomvc/src/model/cadastro.php';
class CadastroController {
private $cadastroTO;
private $cadastro;
public function __construct() {
$this->cadastroTO = new CadastroTO();
$this->inicializa();
}
/*
*
*/
public function inicializa() {
$codigo = (int) $_POST['codigo'];
$nome = (String) $_POST['nome'];
$this->cadastroTO ->setCodigo($codigo);
$this->cadastroTO ->setNome($nome);
$this->cadastro = new Cadastro($this->cadastroTO);
try {
//variável a ser passada para a pagina.
$dados = $this->cadastro ->consultar();
header("Location: " . $_SERVER['DOCUMENT_ROOT'] . '/exemplomvc/index.php');
}
catch(CadastroException $erro) {
echo $erro ->getMessage();
}
}
}
$obj = new CadastroController();
When this class is called, it redirects to a page that will display the html, and on this page I would like to retrieve this variable "$data" that contains an instance of the class with the gets and sets.
It would be the best way to start the session variable?
You can use session or Hidden input if there are few values. Show the code you have already done.
– rray
You can do it with javascript. If you want, use JSON. If not, with php just save the data within a Session, which you will use on more than one page (or on non-consecutive pages in the future), you do not need to keep passing the data via form.
– HiHello
I added more details, including the example class to explain it better. It would be a good solution to start a session variable?
– Raphael Ribeiro
Look to read about Query string, I think it can help you.
– Marconi
It may sound like a stupid question, but what exactly do you mean by redirecting? Through the code, none is being done and without this information, not only is your question meaningless, but we can end up answering something that makes less sense yet.
– Bruno Augusto
Hello @Brunoaugusto At the end of Try I would like to run a header: header("Location: " . $_SERVER['DOCUMENT_ROOT'] . '/exemplomvc/index.php'); E in index.php where it was redirected I would like to retrieve the "$data" variable. The "$data" variable contains an array of objects.
– Raphael Ribeiro
You have two output then. Store the property object register in a session and record it immediately and only then redirect -OR- solve every problem in this method, of this class and only redirect if there is a success, after all this is one of the functions of the Try block.
– Bruno Augusto
instead of redirecting, I think I could do a include.. , so the object would be accessible in the script within "/exemplomvc/index.php".. Of course I do not know what is in this index.php... then you should evaluate... Personal opinion, I would avoid this redirection as it is.. I find it unnecessary and only consumes more requests to the server, despite being little thing, but consumes..
– Daniel Omine