Understanding about MVC

Asked

Viewed 288 times

1

I’m trying to understand the MVC pattern but there are so many videos differently on the web that mess the understanding.

I, in turn, am using a pattern of my own. But I would like to know if it is correct, otherwise, what would be the proper form.

I’m using in 2 layers of classes and one in a separate file.

Example:

Table: Customers

MVC/Model/Clients.php

class Clientes {
 $id;
 $nome;
 $idade;
 public function __construct() {}
/**
  Getteres e Setteres
**/
}

MVC/View/Clients.php

class Clientes {
 public function __construct() {}
/**
  métodos de gravação,alteração e leitura no banco
**/
}

MVC/Controller/Clients.php

require_once '../Controller/Clientes';
require_once '../Model/Clientes';

$clientes = new Clientes();
$clientesLista = $clientes->listaClientes();

index php.

require_once "MVC/Controller/Clientes.php";

Is that the idea? Or am I totally on the outside?

If this is not the case, please post a very simple example like the one I posted. Preferably using terms known as class Clientes and please avoid examples like Class foo, foo->bar.

  • 1

    In his View you are putting methods to "recording", and that is not the purpose of View, it must be "what the user sees" in a simple way, i.e., it must receive the Model and generate the html for the user. The methods to save/read data must be in the Controller

  • See https://answall.com/questions/114824/como-e-para-que-usar-mvc-no-php/116392#116392

2 answers

7

You have just discovered the wonderful world of training on the web (and some outside of it), mainly, but not limited, to free ones. A lot of misinformation being passed on, little questioning and discretion. When those who learn do not understand what is happening there, can not evaluate whether it is right and is saying more so, in everything in life. Disinformation prevails and those who do not have a very good basis to classify the overall quality are lost even.

What matters is to attend to your need well. Saying you did MVC just by saying and this not being the best solution does not help at all.

You can do MVC without using classes. Haven’t you ever seen someone show it like that? Because probably everyone who has seen it teaching this pattern doesn’t know it. They teach something they don’t understand. But there are exceptions, of course, it may just be the approach of that moment that one is going through. But how will we know?

The MVC has no universal application and many cases only bring bureaucracy without coming together some advantage. Actually most of what I see around doesn’t need controller, and he is there in embellishment. Indeed it is so obvious that some frameworks offer alternatives, such as Razor Pages no . NET.

In cases where it is useful it should not have a necessarily certain implementation, only one appropriate to the context.

Its first two code seem repeated and most of them unnecessary, although I can not say because it is full of comment and no code.

Getters and setters are needed there? Do they give you any advantage? In scripts does not usually give. If your application is not a script probably shouldn’t use a language of script to do so.

To view should have presentation and nothing else. There it seems that will put things that should be in the model, should be in the model or in an auxiliary service.

The controller seems to have got it right, I don’t know if it will continue, but its code doesn’t indicate this.

The example will teach you nothing useful because it is just another cake recipe that may or may not be suitable for your problem, which is not well defined to be able to say something. In context use Clientes or Foo is the same. In fact Clientes can be worse because it can pass the impression that there is something practical, useful, when in fact it is not. Almost always example without grounds harm the understanding, and this is one of the problems of videos, tutorials and pseudo courses, besides of course, of books slots. You only learn when you start from the beginning.

Here is some information, but in a very organized way. If you read all, follow the links, consult the past bibliography, can prepare for the MVC. But before it would be good to understand other aspects of computing and thinking. There are some cases of failures and holes and even my answers are not so good. Some links (I don’t sign under a few):

  • https://answall.com/q/114084/18246 - it may also be useful

  • @LINQ Wants to?

2


The layer Model is where all the "Business Model" of your application is located. This also includes methods of recording, altering and reading in the bank.

The layer Controller is a bridge, the initial access of the user before accessing the View and the Model.


Example structure:

  • MVC/Model/
    • Php clients.
    • Clientesdao.php
  • MVC/View/
    • Clientesview.php
  • MVC/Controller/
    • Clientescontroller.php
  • index php.

index php.

require_once "MVC/Controller/ClientesController.php";
new ClientesController();

MVC/Controller/Clientscontroller.php

require_once '../Model/Clientes';
require_once '../Model/ClientesDAO';
require_once '../View/ClientesView';

class ClientesController {

    public function __construct() {

        ClienteDAO cli = new ClienteDAO();
        $listagemDeClientes = cli -> listar();

        $view = new ClientesView();
        $view->exibir($listagemDeClientes);
    }

}

MVC/Model/Clientesdao.php

class ClientesDAO {

    public function __construct() {

    }

    public function listar(){
        /// conectar com o banco
        // gerar a lista
        $arrayClientes = [

            new Clientes("Nome1", "Sobrenome1"),
            new Clientes("Nome2", "Sobrenome2")

        ];

        return $arrayClientes;

    }

}

MVC/Model/Clients.php

class Clientes {

    private $nome, $sobrenome;

    public function __construct($nome, $sobrenome) {
        $this->nome = $nome;
        $this->sobrenome = $sobrenome;
    }

    public function getNome(){
        return $this->nome;
    }

    public function getSobrenome(){
        return $this->sobrenome;
    }

}

MVC/View/Clientesview.php

class ClientesView {

    public function __construct() {

    }

    public function exibir($lista){

        foreach($lista as $clientes){
            $conteudo = file_get_contents("../htm/template1.php");
            $conteudo = str_replace("{nome}", $clientes->getNome);
            $conteudo = str_replace("{sobrenome}", $clientes->getSobrenome);
            echo $conteudo;
        }

    }

}

htm/template1.php

<h1>{nome}</h1>
<p>{sobrenome}</p>

You can implement more than 1 Controller if you need to. But note that the Controller only identifies the request and "chat" with the rest of the application. The entire business model is within the model layer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.