Home arrow PHP arrow Page 3 - Building a Content Management System with Code Igniter

Building a controller class - PHP

Welcome to the conclusion of a nine-part series focused on showing you how to build applications with the Code Igniter framework. Designed to allow programmers to create applications rapidly by utilizing the Model-View-Controller pattern, the Code Igniter framework lets you add many capabilities quickly and easily. This article will bring everything we've learned together for the creation of a content management system.

  1. Building a Content Management System with Code Igniter
  2. Creating sample MySQL tables and defining a model class
  3. Building a controller class
  4. Completing the CMS with view files
By: Alejandro Gervasio
Rating: starstarstarstarstar / 14
October 08, 2008

print this article



True to form, building a controller class that fetches all the movies included in the previous “movies” MySQL table, and also allows users to enter comments about each of them, simply involves defining a different method for performing each of these tasks.

However, you will gain a better understanding of the way that this controller class is going to work if you examine its signature, which has been included below. Take a look at it, please:

class Movies extends Controller{

function Movie(){

// load controller parent


// load 'MovieModel' model


// load helpers




// display all movies

function index(){

$data['title']='Movies List';


// load 'movie_view' view



// display all comments

function comments(){

$data['title']='Comment List';


// load 'moviecomment_view' view



// insert comment

function insert_comment(){





Although the definition of the above “Movie” controller looks pretty intimidating at first, it's actually pretty simple to understand. First, its constructor loads all of the source classes and functions that will be used within the management system; this includes the corresponding model, as well as the “url” and “form” helpers that you learned in previous articles of this series.

Now, take a look at the “index()” method. As you’ll recall, it will be called automatically by Code Igniter when implemented, and it is tasked with displaying on screen all of the movies included in the “movies” MySQL table defined before.

As you can see, this method first fetches all of the movies from the aforementioned table by means of the model, and lastly it embeds this data into a view file called “movie_view.php” for displaying purposes. For right now, don't worry about how this view looks, since this topic will be discussed in depth in the following section.

At this level, you hopefully grasped the logic implemented by the pertinent “index()” method, right? So focus your attention now on the one called “comments().” As its name suggests, this method is responsible for fetching all of the comments that belong to a specific movie. This task is performed by means of a conditional WHERE SQL statement, which is logically hidden behind the model’s API.

In addition, you should notice that this method retrieves the comments made on a particular movie by using a parameter passed in within the URL, whose value is returned by the following expression:


Actually, Code Igniter will automatically load a class called “uri” with each controller defined, so in this case its “segment()” method is utilized to retrieve the ID of the movie that has received a comment.

And finally, the “insert_comment()” method again uses the model’s API, this time for adding a new entry to the “comments” MySQL table created in the previous section. Of course, the text and author that correspond to each comment are collected via a post HTML form, therefore the following expressions:



first add the comment in question to the corresponding “comments” MySQL table, and then redirect users back to the comment’s web page.

Now, before I forget, please save the controller class to the Code Igniter /system/application/controllers/ as “movies.php” folder for further use.

So far, so good, right? At this point, you should have a clear idea of how the previous “Movies” controller class does its thing. Also, it’s possible that you still have some doubts regarding the way that movies and comments are displayed on the browser, or even how new comments are submitted by a user.

However, you shouldn’t be too concerned about this. In the next section, I’ll be listing all of the view files that are required to perform all the aforementioned tasks.

Want to see how this will be done? Click on the link below and keep reading.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: