Lynda. Up and Running with PHP CodeIgniter / Section 2 - 2. Introducing the MVC development pattern

  • 10 years ago
Introducing the MVC development pattern

I'm going to explain MVC at only a very high level as this courses focus is on CodeIgniter . For a more comprehensive description I recommend MVC Frameworks for Building PHP Web Applications with Drew Falkman here in the lynda.com online training library. MVC stands for Model-View-Controller. It's a software design pattern that separates the representation of information from user interaction. This explicit separation of components allows for greater code reusability and logical separation of program responsibilities.

In short MVC keeps code organized, which in turn helps projects and teams. CodeIgniter uses a loose approach to MVC which I'll get into more detail in a bit. There are three components to MVC. The first, model is the representation of data structures, business rules, and functions. This is where really basic, create, read, update, and destroy or CRUD functionality is defined. Next is a View, which is a representation of the information that's presented to the user. The View is where all HTML rendering takes place.

The most program logic that's in a View is a really simple if, then, like if something exists then display it. Anything that takes more logic than that, like deciding what to do based on user input, is delegated to the controller, which serves as kind of an intermediary between the model and the view. The controller sends commands to the other components like model, save yourself, then view, show a message saying that the model was saved. As a visual explanation of a regular interaction, a site user will send a command to the controller which will in turn manipulate the model, like create this record, for example. Through which the control updates the View with a confirmation that the record has been updated.

The User sees the final HTML representation in the View. Earlier, I mentioned that CodeIgniter uses a loose approach to MVC. To be clear, CodeIgniter's use of models is unique and does not precisely fit in traditional definition within a model MVC design patterns. They're more like a domain object, which is a collection of properties. Models in Codelgniter are optional. Depending on your project needs, you can completely leave it out, leaving only a minimal application with just controllers and views. I'm going to demonstrate Codelgniter's implementation of models. Now that we've got this theory out of the way, let's start building an application by installing the Codelgniter framework.

Recommended