Controllers¶
Controllers are the main part of your application, as they determine how HTTP requests should be handled.
Page Contents
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, Codingox 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:
<?php
class Blog extends Base_Controller {
public function index()
{
echo 'Hello World!';
}
}
Then save the file to your controllers/ directory.
Important
The file must be called ‘Blog.php’, with a capital ‘B’.
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!
Important
Class names must start with an uppercase letter.
This is valid:
<?php
class Blog extends Base_Controller {
}
Also, always make sure your controller extends the parent controller class so that it can inherit all its methods.
Methods¶
In the above example the method name is index()
. The “index” method
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:
example.com/index.php/blog/index/
The second segment of the URI determines which method in the controller gets called.
Let’s try it. Add a new method to your controller:
<?php
class Blog extends Base_Controller {
public function index()
{
echo 'Hello World!';
}
public function comments()
{
echo 'Look at this!';
}
}
Now load the following URL to see the comment method:
example.com/index.php/blog/comments/
You should see your new message.
Passing URI Segments to your methods¶
If your URI contains more than two segments they will be passed to your method as parameters.
For example, let’s say you have a URI like this:
example.com/index.php/blog/article/10
Your method will be passed URI segments 3 (“10”):
<?php
class Blog extends Base_Controller {
public function article($id)
{
echo $id;
}
}
Important
If you are using the URI Routing feature, the segments passed to your method will be the re-routed ones.
Defining a Default Controller¶
Codingox 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 config/router.php file and set this variable:
$route['default'] = '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.
For more information, please refer to the “Reserved Routes” section of the URI Routing documentation.
Private methods¶
In some cases you may want certain methods hidden from public access. In order to achieve this, simply declare the method as being private or protected and it will not be served via a URL request. For example, if you were to have a method like this:
private function _utility()
{
// some code
}
Trying to access it via the URL, like this, will not work:
example.com/index.php/blog/_utility/
Note
Prefixing method names with an underscore will also prevent them from being called. This is a legacy feature that is left for backwards-compatibility.
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::__construct();
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.
Example:
<?php
class Blog extends Base_Controller {
public function __construct()
{
parent::__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.
Important
You should also never have a method named identically
to its class name. If you do, and there is no __construct()
method in the same class, then your e.g. Index::index()
method will be executed as a class constructor! This is a PHP4
backwards-compatibility feature.