Navigating Your Site

Navigating Your Site

MVC—Just Another Acronym?
MVC is a means of organizing a dynamic website. The design pattern has been
around since 1979 when it was first described by the Norwegian, Trygve Reenskaug.
Here's an outline of the different types of files:

• Models are objects, which represent the underlying data. They hover above
the database and access it as required. They can also perform operations on
data to add meaning to it.

• Views show the state of the model. They are responsible for displaying
information to the end user. (Although they are normally HMTL views, they
might be any form of interface. They might be views specially adapted for
small PDA screens or WAP telephones, for example.)

• Controllers offer options to change the state of the model. They are
responsible for consulting models. They provide the dynamic data to views.

CI has subfolders for models, views, and controllers. Each file within them is a
.php file, usually in the form of a class that follows certain naming conventions.

CI helps you to follow the MVC pattern, and as a result makes it much easier to lay
your code out. CI allows you a lot of flexibility, and you get all the advantages of the
MVC structure.

Try to think in MVC terms as you write. As far as possible, try to keep your 'views'
focused purely on presentation, and your 'controllers' purely on controlling
application flow. Keep the application logic in the data models and the database.

This way, if you do decide to create a new set of views for a new display method,
you don't have to alter much code in any of the controllers or the models. If you
update some of your 'business logic', you only have to change code in the models.

On the other hand, while this is a very interesting and useful division, it's important
not to take it too seriously. MVC is intended to help you and not to be a straitjacket.
Different programs and frameworks implement MVC in slightly different ways.
The CI forums contain many anguished queries about the 'right' way to implement
MVC. (Should I do database queries from controllers, or should this only be done
in models? Can I return a view directly from a model, or should I go through a
controller first?)