2
I’m starting with programming and I’m doing a CRUD with MVC and DAO. I would like to know if the form I am doing is correct, what I could improve and how I call the insertion method of Productocontroller in the form.
<?php
class Database {
private $host = "localhost";
private $username = "root";
private $password = "123456";
private $database = "crud";
public function conecta()
{
$conexao = new mysqli($this->host, $this->username, $this->password, $this->database);
return $conexao;
}
}
<?php
class Produto {
private $id;
private $nome;
private $descricao;
private $preco;
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
}
public function getNome() {
return $this->nome;
}
public function setNome($nome) {
$this->nome = $nome;
}
public function getDescricao() {
return $this->descricao;
}
public function setDescricao($descricao) {
$this->descricao = $descricao;
}
public function getPreco() {
return $this->preco;
}
public function setPreco($preco) {
$this->preco = $preco;
}
}
<?php
class ProdutoDAO {
function adiciona(Database $conexao, Produto $produto) {
$query = "INSERT INTO produtos (nome, descricao, preco) VALUES ('{$produto->getNome()}', '{$produto->getDescricao()}', '{$produto->getPreco()}')";
mysqli_query($conexao->conecta(), $query);
}
}
<?php
require_once ('../models/Produto.php');
require_once ('../models/ProdutoDAO.php');
require_once ('../config/Database.php');
class ProdutoController {
public function insere() {
$nome = $_POST['nome'];
$descricao = $_POST['descricao'];
$preco = $_POST['preco'];
$conexao = new Database();
$produto = new Produto();
$produto->setNome($nome);
$produto->setPreco($preco);
$produto->setDescricao($descricao);
$produtoDao = new ProdutoDao();
$produtoDao->adiciona($conexao, $produto);
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="../../public/css/bootstrap.min.css" rel="stylesheet">
<title>Adiciona Produto</title>
</head>
<body>
<div class="container">
<h3>Adicionar Produto</h3>
<form method="post" action="../../controllers/ProdutoController.php">
<div class="form-group">
<label>Nome</label>
<input type="text" class="form-control" name="nome">
</div>
<div class="form-group">
<label>Descrição</label>
<input type="text" class="form-control" name="descricao">
</div>
<div class="form-group">
<label>Preço</label>
<input type="text" class="form-control" name="preco">
</div>
<button type="submit" class="btn btn-primary">Adicionar</button>
</form>
</div>
</body>
</html>
All that code is in one file ?
– Mauro Alexandre
No. Each class has its own file. Where I open <? php is a file.
– Marcelo
I edited to separate.
– Marcelo
Marcelo you are not messing with MVC purely neh, is it a way you think it is MVC? (I judge this because this layer of
model
which is the relationship with bank) is almost good, with adjustment improvement and a lot. What I’m not getting is that you’re messing with files finished with.php
and calling in the browser? Give an explanation!– novic
I’m starting to see these patterns, and I don’t think they’re in the best shape, so I wanted tips on what to do. I don’t understand what you mean by calling in the browser? There is a form that I register for a product, that would be it. Only I don’t know how to call the insert method of Productocontroller.php So there is no php in the html form.
– Marcelo
Yes Marcelo you’re calling it in
action:../../controllers/ProdutoController.php
! Nothing prevents you from working like this, is not wrong, does not mean that it is purely MVC, but, you have made your own division. Note filename.php
we usually put everything into minuscules.– novic
I get it, this is a detail. My problem is to make it work. As I said it is not perfect. How do I send the form data to the Productocontroller?
– Marcelo
I did a roundup on what I think could be improved and how to utilize by
<form/>
. How are you usingmysqli
I did in procedural I find it simpler and easier to understand.– novic