I want to offer an easy course of articles that would help beginners, and for oldies, would cause only a warm smile on their face over the past years, aimed at simply mastering the concept of MVC.
Concept
A course of articles designed to tell a newbie about how such terrible giants as
PHP MVC Framework actually work.
The course in no way claims the title of
"Comprehensive Guide: " Do it yourself everything that has long been invented " , but it will help you understand the smallest and only novice programmers in the web world, how it is all written, and even works. But before you download tons of code, you need to understand the very concept of MVC. What it is?
And now, PHP pros can no longer tolerate without inserting: "
Again this is a banal about the basics, patterns, etc. ... " Calm down, my dear, do not forget that once you did not understand half of what you have now causes uncontrollable laughter, once and you had to sit with full sorrow eyes after the next "
@include (); " and it was you who once also did not know about the everyday "
Model-View-Controller ". Therefore, let's not sneer at the small and just beginning PHP users and let them feel like the same
artists , or even the creators of their code, like you are already! And also do not forget, dear gurus, that repetition is the mother of learning.
')
Now for the livelihood.
MVC is just a set of design patterns (patterns) of your application, which involves splitting your code into three components:
Model (
Model ),
View (
View ),
Controller (
Controller ). Simply put, MVC is a set of patterns that allows you to share a little confused code, into three independent parts -
data-type-logic . That's all! Perhaps you have long used such an application architecture, but did not know such a beautiful name. But now you can straighten your shoulders a little, inflate your chest with a wheel and lift your head up with the words: “
I still have nothing! ”.
But this is still not enough, you should sort out a little with each of these three whales, so let's quickly clear the field for ideas and unfold a
colorful and vivid picture of the MVC universe.
So, all that we see on the page that you can touch, move - this is the
presentation . It may include HTML markup, scripts, styles, and other user interface elements. But the performance is just a stage of our theater, which has nothing more than beautiful design. Yes, the scene can be beautifully decorated, with a good setting of the light, the scenery can be performed from stunning materials, but no one needs it without the actors, it is a
rustling wrapper .
The actors in our theater are actors who long before the premiere are preparing for their role; monologues rehearse for hours, talk, play with eyebrows. Actors like everyone else know everything about their character. These actors are
the MVC concept
models . Each model has a set of data that can be operated on using specific methods.
Here we have a stage, there are actors ..., it would seem, what else is needed for a fascinating performance? I think it lacks the original
script . Script writers should put everything in its place: tell the actors where to play, arrange harmoniously the scenery, maintain a logical chain of events throughout the performance. You, probably, already understood that it is a question of
controllers , on whose shoulders support of communication of the user with the application lies.
Now our performance is ready to applause the audience, who are delighted with the production.
But in practice, to achieve such ephemeral success is not immediately, because the concept of MVC goes far beyond these three letters, as far as the implementation and application of this set of design patterns. At first,
it is difficult to connect three whales , and even more succinctly describe the controller, which would not be like a separate module containing an infinite number of business logic, heaps of checks and validators. Add to all this the whisper of "
skilled pens " when working with models, and voila - a freshly baked piece of "
fat controller " was served on the table. There is nothing terrible in this, and even offensive, dear programmers, because if you saw it, it means you are growing!
And by the way, what is a
fat controller ? The term "
Thick Dumb Ugly Controller " appeared in protest against the ardent desire of programmers to dump all operations with data on the controllers. Yes, yes, lay the inimitable play of the actors on the shoulders of the writers.
The first published MVC concept stated that controllers should act as a "
layer between models, views, and input queries ." However, in the aftermath, in the 1990s, the terms of the three whales were rethought, after the release of Ivar Jakobson’s book "
Object-Oriented Software Engineering: A Case Driven Approach, " which described a system similar to MVC, but implementing completely different goals. . For example, the Jacobson controller should act as a container for other objects of the subsystem. Feel the difference? Therefore, dear kids, perk up if at first you get fat controllers. You can easily fend off the situation by folding your arms across your chest with the words: "
I just stick to a different concept ."
The subtlety and elegance of the good controllers is precisely the ability to act as an intermediary between the two other whales.
Now back to our "
actors ". Imagine a performance in which all the actors are only engaged in reading the learned roles, without decorating with emotions all the brightness of happiness or the depth of the tragedy of the events taking place. Sad, isn't it? .. And now let's imagine the models that are engaged only in this, that they play the role of
dull actors without performing any actions on the data, except by implementing a primitive access to the database. Do you think that if someone places his responsibilities on another, will it be good?
Correct answer: TTUC.You just need to
feed the model to the desired state, cutting out the superfluous from the controllers, in order for everyone to do their work.
As for the scenery of our scene, programmers are dear, then let's remember for a second a replica from a good Soviet cartoon: “
You are what you will be for me? ” ... You shouldn’t shift to the performance differently unrelated things .
Of course, the concept of MVC can be split up to each applicable template in the frame maker; the main purpose of this article was to
easily and unobtrusively report brief but colorful information about such a daily concept as
Model-View-Controller .