PHP for the little ones. MVC in its own way

I want to offer an easy course of articles that would help beginners, and for old people I would only evoke a warm smile on my face over the years I lived in, aimed at simply mastering the concept of MVC.

Concept


A course of articles designed to tell a beginner about how scary giants such as PHP MVC frameworks are actually arranged .

The course by no means claims the title of “Comprehensive manual: “ Do it yourself everything that has been invented a long time ago , but it will help the smallest and only novice programmers in the world of the web understand how it is written, and even works. But before you load tons of code, you need to understand the very concept of MVC. What it is?

And now, PHP pros can no longer endure without inserting: " Again this triviality about the basics, patterns, and more ..."Calm down, my dears, do not forget that once you didn’t understand half of what is causing you uncontrollable laughter, once you had to sit with heartbroken eyes after the next" @include (); "and it was you who once also didn’t know about the everyday" Model-Presentation-Controller . "Therefore, let's not mock the small and just starting PHP developers and let them feel like the same artists , or even creators of your code, which you are already ! And also do not forget, dear gurus, that repetition is the mother of learning.

Now for the essential. MVC- it's just a set of design patterns (patterns) of your application, which involves the separation of your code into three components: Model ( model ), View Take ( Presentation ), Controller software ( controller ). Simply put, MVC is a set of patterns that can be used to split a bit of messy code into three independent parts - data-view-logic . That's all! Perhaps you have used such an application architecture a long time ago, but you 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: “ But I have nothing! ”.

But this is not enough so far, we should deal with each of these three whales a bit, so let's quickly clear the field of view and expand a colorful and vivid picture of the universe of MVC.

So, all that we see on the page that you can touch, move - this is the idea . It may include HTML markup, scripts, styles, and other user interface elements. But the performance is just the stage of our theater, which has nothing more than a beautiful design. Yes, the scene can be beautifully designed, with a good setting of light, the scenery can be made of amazing materials, but without actors, nobody needs it, it is a rustling wrap .

The actors in our theater are actors who, long before the premiere, are preparing for their role; for hours rehearsing monologues, delivering a speech, playing with eyebrows. Actors, like no one else, know everything about their character. These actors are models of the MVC concept. Each model has a data set that can be operated using certain methods.

Here we have a scene, there are actors ... it would seem, what else is needed for an exciting performance? I think the original script is missing . Scriptwriters should put everything in its place: tell the actors where to play, arrange the scenery harmoniously, maintain a logical chain of events throughout the entire performance. You probably already understood that we are talking about controllerson whose shoulders lies the support of user communication with the application.

Now our performance is ready to receive the applause of the audience, who are delighted with the production.
But in practice, achieving such ephemeral success is not immediately possible, because the MVC concept goes far beyond the three letters, as far as implementing and applying this set of design patterns. At first, it’s difficult to connect the three whales , and even more succinctly describe a controller that would not look like a separate module containing an endless number of business logic, heaps of checks and validators. Add to all this a whisper of " skillful pens " when working with models, and voila - a freshly baked piece of a " thick controller"handed over to the table. This is not a big deal, and even insulting, dear programmers, because if you saw it, then you are growing!

And by the way, what is a thick controller ? The term" Fat Dumb Ugly Controller "appeared in protest against programmers' vigorous desire to blame all operations with data on controllers. Yes, yes, to put the inimitable game of actors on the shoulders of scriptwriters. The
first published MVC concept stated that controllers should act as a " layer between models, views, and input requests ." However, later, in the 90s, the terms of the three whales were rethought after the publication of Ivar Jacobson's book Object-Oriented Software Engineering:", which described a system similar to MVC, but realizing completely different goals. For example, the Jacobson controller should act as a container for other objects of the subsystem. Do you feel the difference? Therefore, dear kids, perk up, if you start with it is thick controllers will turn out, you can easily fend off the situation, his hands folded on his chest with the words. " I just stick to another concept ."
the subtlety and elegance good as controllers lies in the possibility to mediate between the other two and whales.

Now on to the " actors"Imagine a performance in which all the actors are only engaged in reading memorized roles without embellishing with emotions the full brightness of happiness or the depth of the tragedy of the events. Sadly, isn't it? .. And now let's imagine models that are just doing that, that they act as dull actors without performing any actions with the data, except by providing primitive access to the database, do you think that if someone assigns his duties to another, will it be good? The
correct answer is: TTUK. You
just need to feed model to the desired state, cutting the excess of the controllers, to each engaged in the business.

As for the scenery of our stage, dear programmers, let’s remember for a second a replica from a good Soviet cartoon: “Well , you will be for me? ” ... You should not shift to the presentation the work of various works that have nothing to do with its nature .

Of course, you can split the MVC concept up to every applicable template in the framework; the main goal of this article was to easily and unobtrusively convey brief but colorful information about such an everyday concept as Model-View-Controller .

Also popular now: