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?
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.
– Jasar Orion
Not if it will help or complicate, but take a look at the concept
ViewModel
ofASP.Net MVC
.– Daniel Omine
Thank you, I’ll take a look at these concepts.
– user3681
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 theinput
, send tomodel
which will return the status of the operation. Depending on the result, thecontroller
invokes theview
correspondent, www.domain.com/successful registration.html or www.domain.com/cadastro.html where errors will be displayed.– Papa Charlie
Almost that...
title
anddescription
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– Papa Charlie
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.
– user3681