Controllers
Controllers are the heart of your application, as they determine how HTTP requests should be handled.
- What is a Controller?
- Hello World
- Functions
- Private Functions
- Defining a Default Controller
- Organizing Controllers into Sub-folders
- Class Constructors
- Reserved Function Names
What is a Controller?
A Controller is simply a class file that is named in a way that can be associated with a URI.
Consider this URI:
www.your-site.com/index.php/blog/
In the above example, Code Igniter would attempt to find a controller named blog.php and load it.
When a controller's name matches the first segment of a URI, it will be loaded.
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 blog.php, and put the following code in it:
Then save the file to your application/controllers/ folder.
Now visit the your site using a URL similar to this:
www.your-site.com/index.php/blog/
If you did it right, you should see Hello World!.
Note: Class names must start with an uppercase letter. In other words, this is valid:
<?php
class Blog extends Controller {
}
?>
This is not valid:
<?php
class blog extends Controller {
}
?>
Also, always make sure your controller extends the parent controller class so that it can inherit all its functions.
Functions
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:
www.your-site.com/index.php/blog/index/
The second segment of the URI determines which function in the controller gets called.
Let's try it. Add a new function to your controller:
Now load the following URL to see the comment function:
www.your-site.com/index.php/blog/comments/
You should see your new message.
Private Functions
In some cases you may not want certain functions accessible publicly. To make a function private, simply add an underscore as the name prefix and it will not be served via a URL request. For example, if you were to have a function like this:
function _utility()
{
// some code
}
Trying to access it via the URL, like this, will not work:
www.your-site.com/index.php/blog/_utility/
Defining a Default Controller
Code Igniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'Blog';
Where Blog is the name of the controller class you want used. If you now load your main index.php file without specifying any URI segments you'll see your Hello World message by default.
Organizing Your Controllers into Sub-folders
If you are building a large application you might find it convenient to organize your controllers into sub-folders. Code Igniter permits you to do this.
Simply create folders within your application/controllers directory and place your controller classes within them.
Note: When using this feature the first segment or your URI must specify the folder. For example, lets say you have a controller located here:
application/controllers/products/shoes.php
To call the above controller your URI will look something like this:
www.your-site.com/index.php/products/shoes/123
Code Igniter also permits you to remap your URIs using its URI Routing feature.
Class Constructors
If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it:
parent::Controller();
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.
If you are not familliar with constructors, in PHP 4, a constructor is simply a function that has the exact same name as the class:
<?php
class Blog extends Controller {
function Blog()
{
parent::Controller();
}
}
?>
In PHP 5, constructors use the following syntax:
<?php
class Blog extends Controller {
function __construct()
{
parent::Controller();
}
}
?>
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.
Reserved Function Names
Since your controller classes will extend the main application controller you must be careful not to name your functions identically to the ones used by that class, otherwise your local functions will override them. The following is a list of reserved names. Do not name your controller functions any of these:
- Controller
- CI_Base
- _ci_autoload
- _ci_autoloader
- _ci_assign_core
- _ci_initialize
- _ci_init_database
- _ci_init_scaffolding
- _ci_is_loaded
- _ci_load
- _ci_scaffolding
- _ci_set_view_path
If you are running PHP 4 there are some additional reserved names. These ONLY apply if you are running PHP 4.
- CI_Loader
- config
- database
- file
- helper
- helpers
- language
- library
- plugin
- plugins
- scaffolding
- script
- view
- vars
That's it!
That, in a nutshell, is all there is to know about controllers.