PHP Tutorials

MVC Design Pattern for PHP

This is a software architecture or design pattern, mainly used in user interface implementation. It is used widely in software engineering. MVC separates an application into three intertwining parts in order to separate representation of information in a way that the user can interpret and understand it. In below, we will explain how to implement MVC Design Pattern in PHP.

The interactions

The designer can define three main components in MVC. The following are the components.

Model: this is the name given to the storage data that is used in the overall design. The model should facilitate collecting and viewing of data. This is the bridge between the controller and viewing components, which makes it possible to have the overall pattern. An interesting aspect of model is that it is ‘blind’ this means it has no knowledge or connection to the data when it is in the view or control components. It does not seek or call for a response from the other parts, as its main and only purpose if processing of data that is to be passed on to other parts. The model acts as a caretaker of the data. It ensures that there are no possible ways of the data being compromised or corrupted.

View: this is where request for the data takes place. This is where the data is viewed and final output is decided. In addition, this part is also associated with controllers when there is a change in the state of data.  This enables the programmer or designer change the set of commands. In many cases, it turns out that implementing MVC is a passive process, hence polling the model for updates, as opposed to being notified.

Controller: this is the last part of the interactions. Its core function is to handle the data being submitted by the users. The user, is the support of the controller and without the user interacting with the controller, it is useless. In other words, the controller can be viewed as the information collector. This makes the controller a one way Data Street, meaning it is connected to one point of view. In addition, it is important to keep in mind that the controller only executes tasks after user interaction with the view.

Uses in web applications

Surprisingly, MVC was developed for use on the desktop. Today, it continues to be widely used and adapted to fit in the design of web applications. In addition, it has been re-designed to accommodate a variety of languages. The initial MVC frameworks consisted of the thin client structure. This placed a lot of emphasis on the serve by placing the controller, model and view components on the server. The following are components of MVC.

Templates

Enforcing the principle of DRY becomes simpler, with the help of the template system. This simplifies use of codes without going through the trouble of rewriting the codes. As users venture into using this technique, they need to be extra careful to avoid encountering a complicate syntax. In some cases, users have to learn a new language, in order to render a small piece of information.

DRY

The philosophy of Don’t Repeat Yourself works perfectly in this scenario. DRY is also used in ruby on rails and other codes. DRY prevent reusing codes and ensures every code is represented in the right way.

Convention vs. Configuration

This design archetype minimizes the amount of decisions the designer makes. This simplifies everything because the only things developers do is changing the necessities.

Something captivating with MVC is that is expresses the root of the problem and allows adaptation into the system through direct management of information.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.