Store content / variables in Model - PHP MVC

Asked

Viewed 497 times

4

Hello,

I’m starting to study the MVC standard for PHP. I’ve read several questions here at Stackoverflow, I’ve studied the Laravel documentation and read several articles.

I chose the concept covered in this post as a basis - https://www.sitepoint.com/the-mvc-pattern-and-php-1/

From this arose a first doubt:

I’m creating a simple website, initially without a database. I have only a few variables for each page ($title, $tags, $Description, etc), an array of values and foreach. Should I store all these variables in my Model pages? (e.g. Homemodel, Sobremodel, etc.) and move directly to the view?

The diagram below checks?

Diagrama

I ask this question, because I have seen in several tutorials the variables being stored in the Controller, as well as the Views being triggered in the Controller. And the Model being used basically for questions of access and request to Database.

Thank you!


UPDATE:

All right, I think I get it. I’ll take a look at the question you sent too. Thank you!

Just to illustrate how I’m doing at the moment:


I got on my route:

$route->get( '/', 'HomeController@index' );

In Homecontroller I have this:

class HomeController {

        public function index() {

            $model = new HomeModel;
            return view( 'home', $model );

        }

}

And finally on Homemodel I have:

class HomeModel {

    public $title;
    public $description;

    public function __construct() {

        $this->title = 'Título do meu site';
        $this->description = 'Descrição do meu site';

    }

}
  • in good MVC is great but I prefer to organize myself by MVP ( model,view,present) the only difference he that in mvp the view or in case the present does not access the model directly, so it is much more organized. I would store the variables in the controller by this is the function of it.

  • Not if it will help or complicate, but take a look at the concept ViewModel of ASP.Net MVC.

  • Thank you, I’ll take a look at these concepts.

  • Let me try something simple. The user accesses home and there is a link to register - for example - www.domain.com/cadastro.html. Your core will carry the register controller, he will receive the input, send to model which will return the status of the operation. Depending on the result, the controller invokes the view correspondent, www.domain.com/successful registration.html or www.domain.com/cadastro.html where errors will be displayed.

  • 1

    Almost that... title and description are part of the view, do not compose the model. In this case these variables would fall better in a type file config, but for the purpose of TEST, use as a global variable and name them in your view. A more usual example would be your model returning any array and you use a loop in view to iterate the elements

  • Even had done this way initially, using defines. But since I have a title ($title) and a description ($Description) for each page. (Home, About, Products, Contact) I did so. I mean, I’ve stored it in the model of every page. My view, is a function that loads the html that is in the view folder, and the second parameter are these variables that I took in the model. In my html file I have the escapes.

Show 1 more comment

1 answer

-1

I recommend that you use these variables in the controllers because the use of models is only necessary when using the database, the ideal is that this information was in the database, but as yet, definitely not in the controller. A view cannot access the model, always have to exist the bridge that is the controller.

  • in MVC the view can access the Model so I commented on MVP

  • It is not what that diagram indicates, and it would also be a deplorable practice to mix the interface bed with the data bed.

  • 1

    The model has nothing to do with (only) database, is the layer where the business rules - handling, validation, etc....

  • So this diagram is not correct?

  • 1

    I would say forget about any diagram and focus on each layer and its responsibility. Understanding this you can identify the functioning of whichever diagram on MVC, right or wrong.

Browser other questions tagged

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