Introduction To Mvc (Model View Controller)

Some people have made mvc very complicated to understand. By writing or defining it in a very hard sense and many people still have confusions about the MVC. Let me define you in very simple words from start. As all of you know that we code or make applications to make our work more easy with the help of computer. And most probably you also know that we write code in logical sense and use database to store information and show some thing to user to interact with that. I hope you are getting me , Let me define you in more simple words don’t worry.

To make an application on the computer we use computer programming. And in computer programming we use logic , database to store some information , and some layout to show something to user just like we have Faceboook. Now image if we are writing the code in a single file. We have to write the logic right ? we have to get and set some information from the database and we have to show some thing to user that is front end work. If we write all these things in a single file our code will be mixed up and will not be easily update able, the person who write this code will also forget how this file was coded by me just because of mes sup of coding.

MVC:

Solution of the above scenario is MVC. Don’t worry I’m here to explain you deeply. First of all MVC stands for Model, View, and Controller. Yes Model, view and controller is the answer of our problem. MVC is a design pattern that is use to write code. It makes the coding work easy because we write the logic in to the controller. All the database work in the model and view is the front end view to show to the user.

Model:

Model is the component which is responsible for all the database actions. For example we need to have all the officers who has the name “John” off course we will get it from database. And Model will do this work because model is made for database completely.

View:

View is the component which is highly responsible to show the frontend to the user. Just like you are reading this post then your view is this page from which you are reading. In simple words to show these types of web pages or any thing you see in a software would be called as the view in the model view controller term.

Controller:

Controller is the main component because it deals all the logic’s in short you can say that core programming we use in the controller. All the main if else conditions that runs the software actually will all be in the controller.

Diagram:

mvc

It was all about the Model,View and controller explanation and I hope you got something from it. Now let’s move on the practical example to understand in a more easy way about the Model, View and controller.

Code Without MVC:

<?php
	
	$conn = mysqli_connect('localhost','root','','ajaxdb');
	if($conn){
	//	echo "successfully created";
	}
	else{
	//	echo "denied";
	}
	$query = mysqli_query($conn,"SELECT * FROM chat");
	$view = "<table border=1>";
	while( $row = mysqli_fetch_array($query)){
		$view .= "<tr><td>Chat Id</td><td>".$row[0]."</td></tr>";
		$view .= "<tr><td>Name</td><td>".$row[1]."</td></tr>";
		$view .= "<tr><td>Chat</td><td>".$row[2]."</td></tr>";
		$view .= "<tr><td>Date</td><td>".$row[3]."</td></tr>";
	}
	$view .= "</table>";
	echo $view;
?>

Now see the code written above all things presents in a single file only. The database , logic and view is also making in a single file that is some how wrong because this kind of practice stuck in certain time of period. Just imagine if much more functionalities added into this file how a programmer will further code. He will face allot of problems and if there is a bug he lefts in the file than it will be very difficult to find out where the problem is.

Code With MVC Pattern:

mvc_coded_pattern

Now check it out how simple and systemic our code looks like in MVC pattern. This is why we need to use this design pattern to make big applications. So that further development and updates can be customize able with neat and clean code with easy way to understand.