Home arrow PHP arrow Page 3 - Building a Database-Driven Application with the Code Igniter PHP Framework

Defining a controller class - PHP

If you’re a PHP developer who’s searching for a framework that lets you build full-blown web applications very quickly and with an easy learning curve, look no further. Welcome to the second part of the series entitled “Introducing the Code Igniter PHP framework.” This series of articles shows you how to use this friendly yet powerful PHP framework to easily develop database-driven applications by using the Model-View-Controller design pattern.

TABLE OF CONTENTS:
  1. Building a Database-Driven Application with the Code Igniter PHP Framework
  2. Retrieving user-related data from a MySQL table
  3. Defining a controller class
  4. Outputting user-related data with Code Igniter
By: Alejandro Gervasio
Rating: starstarstarstarstar / 17
August 20, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the section that you just read, I demonstrated how to build a model class, which came in helpful for retrieving data from a “users” MySQL table. It’s time to build the corresponding controller, which will use the API of the model to directly access this user-related data.

Please examine the signature of this brand new controller class, which looks like this:


class Users extends Controller{

function Users (){

// load controller parent

parent::Controller();

// load 'Users' model

$this->load->model('Users');

}

function index(){

// store data for being displayed on view file

$data['users']=$this->Users->getUsers();

$data['numusers']=$this->Users->getNumUsers();

$data['title']='Displaying user data';

$data['header']='User List';

// load 'users_view' view

$this->load->view('users_view',$data);

}

}


As shown above, I created the previous user controller class by extending the default controller that comes bundled with Code Igniter. Again, don’t forget to save this file to the /system/application/controllers/ folder of Code Igniter for later use.

Besides, there are some important details that you should notice with reference to the way that this class has been defined. First, note how the controller uses the corresponding loader class for loading the user model defined in the prior section.

See how easy it is to instruct a controller to load a particular model? I guess you do! The required syntax for performing this task is the following:


$this->load->model('model_name');


Simple and intuitive, right? Now that you have learned how to include a determined model from within a controller class, it’s time to pay attention to the implementation of the “index()” method, which actually is very interesting. As you can see, it uses the model’s API to fetch all the rows of the pertinent “users” database table, along with the number of records contained in the table in question.

In addition, it’s very important to stress here the notation used for accessing the model’s API. Here’s an example of how to do this:


$data['users']=$this->Users->getUsers();


In this case, since the model has been named “Users,” Code Igniter automatically creates an object that can be referenced as $this->Users. Naturally, if there’s a model called “Blog,” it should be called within the controller as $this->Blog. That’s not too difficult to understand, right?

Now, returning to the implementation of the “index()” method, it finishes its execution by defining some additional parameters, which are first stored in the $data array, and then passed to the corresponding view file for display purposes.

Also, an additional explanation is in order here: please, notice how the controller reuses the loader object for loading the view and populating it with user-related data.

So far, so good. At this stage, I showed you how to build a controller class, which uses the model’s API, to fetch some rows from a sample “users” MySQL table, and to embed this data into a view file.

Nonetheless, if you’re like me, then at this moment you’re wondering how this view file actually looks. Well, in response to that question, in the following section I’ll be creating this view, completing the development of this MySQL-driven application.

Please, click on the link below and keep reading. We’re almost done!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: