Can you decipher this word directly

How to create a kiwi electronic wallet. Kiwi wallet and Webmoney

Bootstrap Framework: Fast Adaptive Loaf

Step-by-step video course on the basics of adaptive layout in the Bootstrap framework.

Learn simply, easily, quickly and efficiently with a powerful and practical tool.

Moving in order and getting money.

FREE Website on WordPress Course

Do you want to master CMS WordPress?

Get the lessons on the design and layout of the website on WordPress.

Learn to work with themes and cut the layout.

Free Video Crew Drawing Site Design, Layout and Installation on CMS WordPress!

* Mouse approval to suspend the role.

Back forward

Convenient approach to web development: MVC model

What is MVC? If you answer briefly, then this is an approach to development that will allow you to achieve structured code and a whole application as a whole.

MVC decrypts as a "Model-View-Controller". Let's talk about it in more detail.

So far, you can choose the two most typical ways to create web applications (sites).

First methodLet's call the "Classic", let's assume that the code of different programming languages ​​and markup is contained in one file.

At the beginning of the file, let's say a database is asked to get information from it. Here we deal with SQL language - special query language that is supposed to interact with the database.

After that, the HTML markup of the page usually begins (where without?). In addition, the PHP code inserts are generated in the right places in the HTML markup that produce the page that the site is its logic. In total we have in the same file: SQL, (X) HTML and PHP. This is already a collection from Salonka. Don't forget to add more CSS and a little bit of JavaScript for the completeness of the image. In the end, in the end, we get such a pulp that in this file in this file is broken into this file.

Of course, the first time you remember it, this is how it happens in it, why, why what is needed, and where to make changes to add / delete / modify certain functionality. However, I guarantee that in a few months' time you will be viewing your code with a confusion, I will remember that it is associated with what changes will "pull out" after changing a file, etc.

I am not saying that you have to completely reject this approach, but it is clear that you have to use it with the spirit and very carefully.

Second way associated with the application of the scheme "Model-type controller".

What is the essence of this approach, and how can it be used to help you with your work?

The main idea of ​​this approach is necessary separation of homogeneous elements through different files. If we say very simply: a file is a language. However, this is a very rude example. That is extremely rare.

Besides the idea of ​​separating different languages ​​into different files, the key concept is also separate files to groups according to the functions they perform in the application.

Here we start with you to take a closer look at the MVC model.

This model implies the separation of all files involved in the development of the site three groups:

1. Files of the group "Model"
2. Files of the group "Controller"
3. Files of the group "View"

It is important here to understand immediately that the name of the MVC system is the conventionality. There can of course be many models, controllers and types in your application (i.e. files that fall under these groups on the listed functions and internal device).

So let's look at each other comparative scheme of the MVC model and the "classic" development technology.


In the left part you can see exactly what we talked about. At the top of the page - SQL database requirements. Then markup plus PHP insertion.

The simplest MVC model scheme is shown on the right. As part of this scheme the models occur on operations related to the interaction with the database: Extracting data, changing and deleting it, counting the number of entries in certain tables, etc.

The controller contains the application logic. What determines its functionality?

The view is designed to be seen by the end user..

Bidirectional arrows on the diagram show that there is a relationship in pairs "Model-Controller" and "Controller - View". Consider this relationship more using the example of the following scheme.


In this scheme we added two new elements: a user browser and a database. In general, consider the entire cycle: When you call a browser at a specific url until the page is displayed to the user:

1. The user changes to the address and the browser addresses the controller.

2. The controller relates to the model.

3. The model relates to the database (e.g. to obtain the information required for the exit)

4. Information from the database falls back on the model.

5. The model information is transmitted to the controller.

6. The controller transmits this information in appearance.

7. The view is displayed in the browser with the controller.

This is the general scheme of the work of this model. As you can see, some of the villas in this system are a browser and a database. Really, the browser can only address the controller because the controller is part of the URL. The visitor cannot come into contact with anyone other than the controller. It is important to understand. A person cannot contact styles or models through the address bar. It only interacts with the controller.

In this regard, we can speak of the controller as a kind of "distribution center". See for yourself: the controller processes the user requests, the controller refers to the model, the controller is an intermediary for outputting the browser.

The second element that the mansion had is the database. And it is right. As part of the MVC concept, it is common only models should work with the base.However, sometimes this principle is broken. In this case, the interaction with the base consists of the controller or even the species.

Of course, you shouldn't wield a stick with a violation of the structural and MVC principles, but sometimes such waste from the rules can be very useful in improving the readability of the code and understanding the scheme of operation of the application.

modelSpeaking of which is an optional element in the MVC schema. It is entirely possible to implement everything that is basically required without using models. In that case, of course, in this case you are interacting with the database from the controller files and the kind. As you understood, it's not a good tone. Since you will soon decide to work within the scope of this concept, it is recommended that you use the models and use them according to their direct intended purpose.

"Extreme", we looked at each other, and in the middle of the system remained our trinity, where the "model controller" and "controller - view" interactions occur.

After studying the fundamentals of this model, you can think about what gives us such an approach as it is classically preferred.

The main money from the application of such a program in his work has already been mentioned - that increased code structure and applications in general. It's no secret that the MVC model is being adopted by many framework manufacturers, including a favorite cluster.

What's the frame? If you utter a strange word, it is only a framework, a certain structure in which you are invited to develop a website. This structure is quite universal to create almost any site with its help. At the same time, what is very important, the framework is very flexible so that you can achieve exactly what you need.

Speaking of another language, Freymovork is a flexible framework that you restrict in terms of structure, but not restrict the function plan.

Returning to the output of MVC, it can be pointed out that a great many frameworks take just such an approach: Give a fairly clear structure of the application in which the division of files by types, models and controllers occurs. All of this taken together is capable of saving you a lot of time once you spend it learning how to use the framework and the MVC model.

MVC models can be taken into account, among other things separator code for function characters. You no longer have to dig into "cases" of SQL queries, markup and PHP code. Whenever you need to fix or change something, you know exactly what kind of file to govern.

Below is part of the file related to the Views group:


But a code of the model:


The controller can look like this:


Very important advantage, as you can see, is branch of the type of code. Often times it is necessary to change the design or even the structure of the site anyway, while maintaining the same information on the previously viewed page. And this is where editing the mix of code begins, which becomes more and more difficult over time.

The advantage of the MVC model is easily possible rule out the correct design of the site when changing the logic of the application. You can change one of the three elements: model, view, controller. At the same time, you don't have to make any changes to other elements, as they are to some extent autonomous.

The problem of autonomy for models and species is particularly relevant. Once, by writing them, you can usually use them successfully on various projects to bring minimal edits or without doing them at all. This can save you a lot of time and eliminate repetition of similar code.

The benefits of using the MVC model within the framework, for example the same customigniter.

It's no secret that every site has a huge amount of similar or even identical features. It is enough to remember the feedback form or the navigation of the page display. These are only the brightest, "external" moments. You can find even more similarities in the code, which is invisible to the common user, in the code running on the server.

Almost all web developers are faced with the need to use similar PHP functions, similar queries to the database, etc. Frame makers were doing one very important thing here - they were trying to group those functions that are most commonly used in separate files by webmasters and web programmers offer new features.

Now you can often do the necessary things that do not give much thought to what to implement. You can write a pair of lines of code instead of a pair of ten lines and save your time and more focus on the logic of the application rather than the implementation of the device.

And all of this happens within the framework of the MVC concept, so you can search almost all of the results through one framework. At the same time you get a high readability of the code. And what else is required for comfortable and efficient work?

Epilogue: Don't forget that any structure created to make it easier to perform certain tasks was created precisely to make the job easier.

Do not adhere to the MVC principle in cases where you are certain that it will affect your understanding of the structure of the application. You shouldn't "get fed up" under the model and it is under you.

Dmitry NAUMENKO.

P. Think what PHP framework to master? Look out for CakePhp - it implements the MVC pattern considered above, and now you can take a small introductory video course to get a general idea of ​​what this framework can do:

Did you like the material and would like to thank you?
Share only with friends and colleagues!


Patter Model View Controller (MVC)Opened in the late 1970s, it is a software architecture design template whose primary purpose is to separate the functions of working with data from its presentation. Theoretically, a competently designed MVC application enables front-end and basin-end developers who cannot intervene in the areas of responsibility of the VC during work, ie the front-end developer does not need to know anything about the "kitchen" of their Bachend-colleague and vice versa.

Although MVC was initially designed to develop desktop applications, it has been adapted for modern tasks and is very popular with web developers because of responsibility, as it enables clear, flawless code due to the sharing of responsibility. The MVC pattern leads to the creation of clear, modular systems that allow developers to make changes to existing code very quickly.

In this article we will consider the basic principles of MVC, starting with defining the pattern and continuing its use in a small example. This article is primarily useful for those who have never encountered this pattern in life, as well as perhaps those who want to refresh their knowledge of MVC skills.

Understand MVC.

As already mentioned, the name of the pattern comes from the abbreviation of three words: Model (model),View (view) and Controller. In short, the principle of the work of the pattern can be illustrated using the same scheme (you can find Wikipedias:

This scheme clearly shows the unidirectionality of the flow of information in the pattern and also describes the roles of each component.

model

The model is used to access and manipulate data. In most cases, the model is used to access the data warehouse (e.g. a database). The model provides an interface for searching, creating, modifying, and removing data from the repository. As part of the MVC pattern, the model acts as an intermediary between the presentation and the controller.

An extremely important feature of the model is that technicians have no knowledge of what happens to the data in the control and representation. The model should never do or expect requirements to / from other components of the pattern.

Always remember, however, that the model is not just a database access gateway or any other system that is in the process of moving data on and on. The model is something like a bandwidth of the data. The model is the hardest part of the system in most cases, partly due to the fact that the model itself is a link to all other parts.

presentation

The representation is where the data obtained from the model will be displayed in the form you want. In traditional web applications developed in the MVC pattern, the view is part of the system in which the HTML code generation is performed. The presentation is also responsible for receiving action from the user to send to the controller. The view shows, for example, the button in the user interface and calls up the corresponding action on the controller after pressing.

There is some misunderstanding about the purpose of the presentation, especially in the web developer environment who are just starting to build their applications with MVC. One of the most commonly broken rules is that the presentation should in no way communicate with the model, and everything the data obtained through the submission should only come from the controller. In practice, developers often ignore this concept in the basics of the MVC pattern. The article Fabio Cevasco clearly shows this confusing approach to MVC in the example of the CakePHP framework, one of many non-standard MVC frameworks:

It is extremely important to understand that in order to get the correct MVC architecture, there should be no direct interactions between representations and models. All data sharing logic between them must be implemented in controllers.

Additionally, there is a common misconception that the view is just a template file. As Tom Butler noted, this bug is due to the fact that from the beginning many developers misunderstand the structure of MVC, after which they begin to pour this "knowledge" on to the masses of novice developers. Indeed, the presentation is much more than just templates, but a lot of frameworks built on the basis of the MVC pattern so skewed the concept of the presentation that everyone doesn't care how correctly their applications come from the MVC pattern .

Another important point is that the view never works with the controller's "clean" data, ie the controller never works with the view to bypass the model.In the interaction between the controller and the presentation, the model should always lie between them.

Regulator

The controller is the last part of the MVC bundle. The controller's job is to get data from the user and the manipulation of the model. It is the controller, and it is the only part of the system that interacts with the user.

In short, the controller can be described as an information collector as an information collector that transfers its models for processing and storage. It should not do anything with the data, but only so that it can only be received by the user. In this way, the controller is connected to a representation and a model, in this way controlling the unidirectional data flow in each phase.

It is very important to remember that the controller begins its work only as a result of user interaction with the representation that the corresponding controller function causes. The most common mistake among developers is that the controller is simply viewed as a gateway between the representation and the model. As a result, the controller is endowed with these functions that should be performed by the presentation (incidentally, where the legs grow from the idea that the presentation is just a template file). In addition to all of them, many fall over all data processing logic at all, forgetting which model is laid out in the MVC pattern.

MVC in PHP.

I recommend implementing the one described above in a small application. Let's start by creating the classes of Model, Representations, and Controller:

Now we have a "skeleton" of the registration class and we need to load it with its methods. We write 2 methods: Set () to set values ​​and Set () to receive values. You can also write the removal ()) method to remove values. Add these methods to the registry class:

FUNCTION SET ($ KEY, $ VAR) (ISSET (Isset ($ this- \ u003e vars [$ Key]) \ u003d \ u003d TRUE) (Throw new exception ("Var cannot be set)". $ KEY. "`. Already discontinued. ");) $ This- \ u003e vars [$ key] \ u003d $ var; RETURN TRUE;) Function GET ($ KEY) (if (if ($ this- \ u003e vars [$ key]) \ u003d \ u003d FALSE) (RETURN NULL;) Return $ this- \ u003e vars [$ KEY];) Remove function ($ var) (UNSET ($ this- \ u003e vars [$ Key]);)? \ U003e

These methods are simple, they install, get, and remove elements from an array of $ vars, which is the class attribute. In the Set () method, at the same time, we check that there is no value with the given key, and if there is, we generate an exception. It is necessary to avoid accidentally rewriting values.

Now we have a full-fledged registration class, but we won't dwell on it. We are using one of the functions of the SPL library: ArrayAccess. SPL (abbreviated from the Standard PHP Library, the Standard PHP Library) is a collection of interfaces and classes designed to solve standard problems. One of the SPL interfaces, ArrayAccess, can be used to provide access to the object as a traditional array. Let's look at this example:

Just created our first controller and when you try to boot the system you can see:

This means that the router class did its job and started the desired action from the required controller. Let's write another controller that meets the "Members / View" requirement. In the controller directory, create a Members.php file and paste such code:

Now go to our MVC system on request "Members / View" or "Index.php? Route \ u003d Members / View". We need to see such a result:

Just writing a new controller and adding a method we could create a new page and nothing had to be changed in the system itself. Additionally, our controllers don't have to plug in the global.php file or do anything.

If we now have controllers, there is only one thing left: "V" or "View" ("Display").

display

As with models, there are several different options for creating the view component in the MVC system. We could teach the router class to automatically download another file called something like "View_ (Name) .php". However, to make the manual clearer, write the Template class that deals with template output.

First, create a Template.php file in the classes directory and paste the following code: