Introduction to Model-view-controller (MVC)

  • MVC, or Model-View-Controller, is an architectural pattern used in software development. The concept of Model-view-controller (or MVC) is a common one and has become increasingly popular as a design framework. MVC can be used for many different kinds of application design.
  • One way to implement an application like this would be for each page to have a corresponding file written in say, PHP. The page for listing users would connect to the database, get back all the users, and place the user information inside HTML that it would send back to the browser. Form submissions would be handled by getting the new settings from the form, and updating the database.
  • The aim of MVC is to promote good programming practices and code reuse by separating a web application into three layers: data, presentation, and the interaction between the two. By separating these elements from each other, one can be easily updated without affecting the others.
  • Having a good understanding of MVC will be extremely helpful for anyone who is interested in learning a web framework .

 

  • Model – This is the part of your application that defines its basic functionality behind a set of abstractions. Data access routines and some business logic can be defined in the model. The model represents the data in the application. The code in the model is often referred to as business logic. It’s important to remember that the model is not the database, it’s an abstraction of the data itself, and everything the application knows about what the data is and how it works is part of the model. The Model’s job is to do ANY data-related related task, from database communication to file access. Then it stores that data inside of itself — all the data needed to construct the page.
  • View – Views define exactly what is presented to the user. Usually controllers pass data to each view to render in some format. Views will often collect data from the user, as well. This is where you’re likely to find HTML markup in your MVC applications. The view is the presentational layer of the application: the user interface. Web frameworks that use MVC usually offer a method of dividing the view into even smaller sections to further modularize code. It selects a View. The ‘views’ are the different display templates for your site. You could have a view for your site’s landing page, a view for a blog post’s comment section, and a view for pages of blog posts.
  • Controller – Controllers bind the whole pattern together. They manipulate models, decide which view to display based on the user’s request and other factors, pass along the data that each view will need, or hand off control to another controller entirely. Think of the controller as the translator between the view and the model. The controller receives requests from the view (the user), decides what to do, communicates with the model as necessary to send or retrieve data, and then prepares a response for the user to be delivered back to the view. In best practice, the controller will not do any manipulation of data or user interface itself, it will merely translate between the view and the model. The “Controller” takes the request and reads it, figuring out what the user wants to see. Looking at the URL and the data that was passed along, it appears that the user is trying to look at page two of your blog. Great, the controller’s main job is now done! It’s identified what needs to be delivered. Now it has two other tasks to do: It has to create the Model, and pass it to the correct View.
  • In short, the model represents the data. The view represents the user interface (the web page), and the controller facilitates communication between the two.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a comment