1.1. What's Model-View-Controller?

Model-View-Controller, as the name implies, is a design pattern that allows developers to cleanly separate their code into three categories:

  • Models maintain data.

  • Views display data and user interface elements.

  • Controllers handle user events that affect models and views.

Figure 1-1 illustrates the relationship among the parts. The important thing to remember is that MVC takes the user into account — it begins with him or her. It's the user who clicks a link, moves the mouse, or submits a form. It's the controller that monitors this activity and takes the appropriate action (such as manipulating the model and updating the view).

Figure 1.1. Figure 1-1

Because of MVC's three-part separation, developers can create multiple views and controllers for any given model without forcing changes in the model design. This separation allows for easily maintained, portable, and organized applications that are nothing like anything you've worked with before.

For example, imagine that most prototypical of early 21st century web applications, the blog. Blogs are everywhere these days, and they're not that hard to break into their constituent MVC parts:

  • A model that keeps track of posts and comments

  • Multiple views that display individual blog posts, a list of blog posts, or search results

  • A controller that captures user interaction (such as clicking on an archive ...

Get Professional CodeIgniter® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.