Is making queries in a view, in the MVC standard, a bad practice?

Asked

Viewed 201 times

1

I know that most PHP frameworks are used by default MVC. Where thing guy (theoretically) should stay in his particular place.

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

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

An example:

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

I notice it gets very disorganized when it occurs.

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

  • Is bad.........

  • It is bad.. Sometimes it happens for example to compare variables with constants and etc.. but in this case it showed.. it is clearly an error

  • Taking into account the MVC standard, because without taking this consideration, it would be based on opinions.

  • It is PHP and Laravel error that, without crying :P

  • Error you speak because it allows it to happen?

  • Cakephp, on the other hand, can’t do a thing like that

Show 1 more comment

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, a query is made directly in the view and some treatment is being done? what happens when an error occurs? In other words a 'low level' code is being excommunicated at a higher level layer. The solution to this is to query the controller and from it dispatch the status list to the view.

  • But it’s a mistake or it’s not?

  • @Wallacemaxters is violated the main idea of the pattern and if you don’t have a good reason for it, it’s an indication that something is wrong.

  • @Wallacemaxters if you find that you need something else you warn me, that I edit the answer later.

0

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

0

I usually compare these cases to someone who drives a vehicle without a license. There is a standard that defines how and who can direct, as much as there is an architectural pattern 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 driving license.

Fact is that this code, at first, 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 license also.

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

Using or not using the default? Here’s the question!

I once participated in a project to rewrite an application because refactoring it would cost more. The pages (hundreds) were full of this type of code, the controllers wrote html hard code in Sponse, in short, it was "impossible" to know who was doing what, such that a visual change (initial scope of the project) became more expensive than redoing it. Particularly I would say ALWAYS use (and correctly) a pattern (MVC or other that is adherent).

Browser other questions tagged

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