What is a controller?

Controllers are classes that can be reached through the URL and take care of handling the request. A controller calls models and other classes to fetch and manipulate the information. Finally, it will pass everything to a view for output.

Creating controllers

In order to function, a controller must do the following:

  • Reside in "application/controllers" folder (or a sub-folder).
  • File name must match the class name exactly, e.g. products.php
  • The class name must map to the file name (with / replaced with _) and each word is capitalized.
  • Must have the Controller class as a (grand)parent.

Some examples of controller names and file locations:

// application/controllers/foobar.php
class Foobar_Controller extends Controller {
// application/controllers/admin.php
class Admin_Controller extends Controller {

Controllers can be in sub-folders:

// application/controllers/users/settings.php
class Users_Settings_Controller extends Controller {
// application/controllers/product/category.php
class Product_Category_Controller extends Controller {

Controllers can extend other controllers:

// application/controllers/users.php
class Users_Controller extends Auth_Controller {
// application/controllers/users/blogs.php
class Users_Blogs_Controller extends Users_Controller {

Let's try it: Hello world!

Let's create a simple controller so you can see it in action. Using your text editor, create a file called "products.php" in "application/controllers" folder, and put the following code in it:

class Products_Controller extends Controller 
	public function index()
		echo 'Hello World!';

	public function comments()
		echo 'My comments!';

Now visit the your site using a URL similar to this:

If you did it right, you should see Hello World!

Controller methods

In the above example the function name is index(). The "index" function is always loaded by default if the second segment of the URI is empty. Another way to show your "Hello World" message would be this:

If we wanted to run "comments" method in the controller, we would do this:

If your controller is in a sub-folder such as "application/controllers/store/products.php" you may use URL similar to this:

Passing URI segments to your controller methods

If your URI contains more then two segments they will be passed to your controller method as parameters. For example, lets say you have a URI like this:

Our controller method would look like this:

class Products_Controller extends Controller 
	public function view()
		echo uri::segment(3) . '-' . uri::segment(4);

We use URI class to fetch third and fourth segments in the URL which are "shoes" and "12".


If you intend to use a constructor in any of your Controllers, you must place the following line of code in it:


The reason this line is necessary is because your local constructor will be overriding the one in the parent controller class so we need to manually call it.

class Products_Controller extends Controller 
       public function __construct()
            // Your own constructor code

Constructors are useful if you need to set some default values, or run a default process when your class is instantiated. Constructors can't return a value, but they can do some default work.