Making queries in a view in the MVC standard is bad practice?

1

I know that most PHP frameworks use the MVC pattern. Where expensive thing (theoretically) should stay in its place.

For example, controllers are responsible for the request and stuff, models are database abstractions, and view is the view layer.

But I've seen some programmers who do, for example, queries using the model within the preview layer.

An example:

  @foreach(Estado::where($usuario->estado_id)->where('status', '=', 1)->get() as $estado)
  <div>{{ $estado->nome }}</div>
  @endforeach

I notice that it gets very disorganized when this occurs.

So I want to know if, Considering the MVC pattern , is this a bad practice, or can it be considered an error on the part of the programmer?

    
asked by anonymous 15.03.2016 / 14:24

3 answers

1

The idea of the MVC standard is to separate the concepts, in the case of the web the code that has some functionality (controller and model) and view that is usually a file with code of little or no logic.

In this example, is an inquiry made directly to the view and is some treatment being done? What happens when an error occurs? In other words a 'low-level' code is being scanned in a layer at a higher level. The solution to this is to query the controller and from it to dispatch the list of states to the view.

    
15.03.2016 / 18:14
0

Considering the MVC standard is a programmer error, although many frameworks allow this practice to work. But not everything that works follows a pattern.

    
15.03.2016 / 17:56
0

I often compare these cases to someone who drives a vehicle without getting a license. There is a standard that defines how and who can direct, as much as there is an architectural standard that defines how and who should have each responsibility in an application, in this case the MVC.

Not respecting the default settings is as wrong as driving without a driver's license.

It is a fact that this code, in principle, works, as well as there are people not enabled driving better than enabled, but starting from a previous definition of standards and responsibility, yes, this code is wrong and who drives without driver too.

Another detail that I think is important is that, in the case of architectural standards, there is no law that requires you to use them, so when you are willing to not apply the standard this same code is no longer wrong .

Use or not to use the default? Here's the question!

I once participated in a project to rewrite an application since refactoring it would cost more. The pages (hundreds) were full of this type of code, the drivers wrote html hard code no response, in other words, it was "impossible" to know who did what, in such a way that a visual change (initial project scope) became more expensive than to redo it. In particular I would ALWAYS say use (and correctly) a pattern (MVC or other that is sticky).

    
15.03.2016 / 18:54