Where to process data in an MVC project

Asked

Viewed 262 times

0

I’m using Laravel as an example. In the Controller i do 2 searches.

public function index()
{
    $page_title = "Relatório";
    $projetos = Projetos::orderBy('alguma_coluna')->get();
    $subprojetos = Subprojetos::get();
    return view('pages.relatorio.index',
        compact('page_title','projetos ','subprojetos ')
    );
}

I need to do a treatment before printing this data on View.

The code below is just an example of data processing

foreach($projetos AS $thisRow) {
    $isDuplicate = false;
    $projeto_nome =  $thisRow["projeto_nome"];
    $hasNewIcon = $thisRow["hasNewIcon"];
    $img_src = $thisRow["img_src"];

    if($hasNewIcon === "1"){
        $hasNewIcon = "<img src='algumaimg.jpg'>";
    } else {
        $hasNewIcon = "";
    }
    foreach ($subprojetos AS $r) {
        $subprojeto_nome = $r["subprojeto_nome"];
        if($contador > 0){
             $isDuplicate = true;
        }
        $contador++;
    }
    $html .= <<<HTML
                     <div class="col-lg-2 col-md-2 col-xs-6 thumb">
                            <img src="{$img_src}" class="img-responsive"/>
                            <div class="equal-height-panels">
                                <div class="row">
                                  <h5 class="text-center">{$projeto_nome}</h5>
                                    {$hasNewIcon}
                                </div>
                            </div>
                    </div>
HTML;
}

My question is: Where should I do this treatment? No Controller, in View or in the Model?

  • It would be interesting for you to do any treatment at Model. And all these parts involving HTML you should put right into the View and do the conditionals there, using the Blade syntax.

1 answer

2


You can use a class responsible for representing your templates for any type of output, this includes html, json and xml. There is a project structure Pattern commonly used in Ruby called MVP (Model-View-Presenter).

Fortunately there is already a library on Github that does this for you, and, it was specially developed for Laravel by Jeffrey Way, I don’t know if you know but it is who writes and records the lessons of Laracasts.

This library allows you to create classes responsible for representing a template for the view. Your code looks more or less like this:

Presenter

<?php
namespace App\Presenters;

use Laracasts\Presenter\Presenter;

class UserPresenter extends Presenter {

    public function fullName()
    {
        return $this->first . ' ' . $this->last;
    }
}

Model

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Laracasts\Presenter\PresentableTrait;

class User extends Model {

    use PresentableTrait;

    protected $presenter = 'UserPresenter';
}

View

<h1>Olá, {{ $user->present()->fullName }}</h1>

This brings many improvements to your code, especially when you need to change the presentation logic of your template.

  • Removes duplicate code.
  • Decreases the code lines within your HTML, at most a call to a function.
  • If you need to modify the way this data is sent to your view, simply modify the method within the presenter class that the entire system accompanies this change.
  • You can apply tests to your class responsible for presenting your model, preventing code breaking.

An extra tip, if one day you come across too much code responsible for building queries within your controller, you can escape to the Repository Pattern.

Browser other questions tagged

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