HomePHP Defining a Model Class for Handling Views with CodeIgniter
Defining a Model Class for Handling Views with CodeIgniter
Welcome to the sixth installment of a seven-part series on handling views with the CodeIgniter PHP framework. This series shows you a few handy approaches that you can implement quickly within your CI-based programs to work with view files in a truly clever way, ranging from loading them sequentially to nesting views within other views.
And now that youíre aware of the topics that are covered in this group of articles, itís time to review the concepts discussed in the last tutorial. In that part, I used a practical example to show you how to partially remove presentation logic from a particular view file, and implement it within a simple controller class.
As with many other aspects related to using the Model-View-Controller pattern with CodeIgniter, making views a bit ďdumberĒ has its pros and cons. How successful this approach can be in the end depends strongly on the structure defined for a particular application. Again, itís fair to mention here that CI gives developers enough flexibility to implement the MVC pattern without forcing the use of a specific programming methodology. Indeed, this is one of its most appreciated characteristics.
Now, returning to the previous tutorial, youíll recall that I developed a sample PHP application. It displayed the contents of some database rows which were previously fetched from a ďusersĒ MySQL table. Moreover, these rows were retrieved by using the active record class bundled with CI.
However, itís also possible to define a model that performs all of the database-related operations, instead of directly using the database class. Thus, in the next few lines Iím going to show you how to build another web application similar to the one that you learned in the preceding article -- but this time it will incorporate a basic model class. In doing so, youíll be able to see how to handle views by using the entire Model-View-Controller triad.
Are you ready to continue learning how to parse views with CodeIgniter? Then letís jump right in!