Last active
September 1, 2015 03:34
-
-
Save fxcosta/fa448f67c0c6d377c523 to your computer and use it in GitHub Desktop.
Exemplo simples criado para exibir qual é a responsabilidade que uma controller deve ter, ou seja, sem regras, apenas delegando para classes de mais alto nível.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ResultadoController | |
{ | |
protected $model; | |
public function __construct() | |
{ | |
$this->model = new ResultadoModel(); | |
} | |
public function calcular() | |
{ | |
// dados oriundos do post | |
$dados = $_POST; | |
// delego a model a responsabilidade de persistir os models de acordo com a sua regra | |
// por que ter regras na controller ultrapassa sua responsabilidade que é somente delegar tarefas | |
// e servir como intermediario entre view e model | |
$resultado = $this->model->calcular($dados); | |
return View::render(['dados' => $resultado]); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ResultadoModel extends Model | |
{ | |
/** | |
* Metodo onde tenho de fato a regra de negocio que me diz o seguinte: tenho que salvar | |
* os dados que recebi do form mas tenho que cadastrar um campo extra que é a mesclagem dos dois inputs recebidos | |
* armazenar isso na base e depois exibir para o usuario o resultado | |
* */ | |
public function calcular($dados) | |
{ | |
$toSave = array(); | |
$toSave['nome'] = $dados['nome']; | |
$toSave['idade'] = $dados['idade']; | |
$toSave['mesclagem'] = ($toSave['idade']+$toSave['nome']) | |
return $this->persist($toSave); | |
} | |
/** | |
* faz de fato a persistencia dos dados | |
**/ | |
public function persist($toSave) | |
{ | |
return $this->save($toSave); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form action="/resultado/calcular"> | |
<input type="text" label="Nome" name="nome" id="nome"> | |
<input type="text" label="Idade" name="idade" id="idade"> | |
<input type="submit"> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment