Skip to content

A simple but effective way of understanding a MVC architecture pattern, specially for the beginners, through an example which is constructed in PHP.

Notifications You must be signed in to change notification settings

annuhuss/Model-View-Controller

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Model-View-Controller(MVC)

MVC is an architecture pattern commonly used for developing user interfaces that divides an application into three interconnected parts, Model-View-Controller. MVC pattern is one of the first approaches to describe and implement software constructs in terms of their responsibilities, especially for developing graphical user interfaces, Wiki.

A short description for a MVC pattern is given below:


Model:

Model is the central component of the pattern which is independent of the user interface. It directly manages the data, for example, fetches the data by interacting with database in the back-end when needed. It also plays with the central logic and rules of the application. The communication between model and controller is an essential part of the MVC.

View:

It is view’s responsibility to display the data when they are available from the controller or the model. View always corresponds to the responses for any user’s requests.

Controller:

The controller handles requests from the users and checks whether the input data are valid, then forwards the data to the model or the view depending on the user input. Overall, it connects to all parts of the MVC pattern.

Routing:

Routing make it possible to get any requests from the users to the controller and sends the responses back to the users.


A MVC Example:

To depict the MVC pattern I have programmed the example using HTML and PHP language. Obviously this example is reflected by the above description.

The index.php file combines all the parts of the MVC example as well as the route/HTTP that produces an authentication request from a user and sends a response for the request to the user.

One needs to execute index.php file. This file operates on the following three files:

  • controller.php
  • model.php
  • view.php

I tried to make the example as simple as possible for describing the MVC and for that reason, I have located the database segment inside the model. Never the less, for a real-world application, you will always work with text-files for a small amount of data or most commonly with databases, e.g. MySQL for dealing with a large amount of data. These data storage will be filled with users information which indicates, they are usually detached from the model but maintain a safe and robust communication between each other.

For the sake of simplicity, I have just stored three pairs of username and password as an admin for determining authenticated users. In this case, each authenticated users certainly enters a pair of username and password out of the set below correctly, so that he or she will have an access to something. On the other hand, trying to access by any illegal user will be denied.

 
 {{'username1', 'password1'}, {'username2', 'password2'}, {'username3','password3'}}
 
 

Hopefully by this article we now have the basic understanding of how a Model-View-Controller architecture pattern works. That's all for today! Next time, I will definitely come with some of PHPs OOP features.

Stars from the audience would be always appreciated.

A detail illustration of this topic and some of my other articles on different topics can be reached by the medium blog site.

About

A simple but effective way of understanding a MVC architecture pattern, specially for the beginners, through an example which is constructed in PHP.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages