Controllers are the heart of your application, as they determine how HTTP requests should be handled.
Consider this URI:
In the above example, CodeIgniter 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.
Then save the file to your application/controllers/ folder.
Now visit the your site using a URL similar to this:
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:
Also, always make sure your controller extends the parent controller class so that it can inherit all its functions.
- What is a Controller?
- Hello World
- Functions
- Passing URI Segments to Your Functions
- Defining a Default Controller
- Remapping Function Calls
- Controlling Output Data
- Private Functions
- 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:
example.com/index.php/blog/
In the above example, CodeIgniter 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:
example.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 CI_Controller {
}
?>
This is not valid:<?php
class blog extends CI_Controller {
}
?>
Also, always make sure your controller extends the parent controller class so that it can inherit all its functions.