Controllers

Controllers are the heart of your application, as they determine how HTTP requests should be handled.

What is a Controller?

A Controller is simply a class file that is named in a way that it can be associated with a URI.

Consider this URI:

  1. example.com/index.php/helloworld/

In the above example, CodeIgniter would attempt to find a controller named Helloworld.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 Helloworld.php,and put the following code in it:

  1. <?php namespace App\Controllers;
  2.  
  3. use CodeIgniter\Controller;
  4.  
  5. class Helloworld extends Controller
  6. {
  7. public function index()
  8. {
  9. echo 'Hello World!';
  10. }
  11. }

Then save the file to your /app/Controllers/ directory.

Important

The file must be called ‘Helloworld.php’, with a capital ‘H’.

Now visit your site using a URL similar to this:

  1. example.com/index.php/helloworld

If you did it right you should see:

  1. Hello World!

Important

Controller class names MUST start with an uppercase letter and ONLY the first character can be uppercase.

This is valid:

  1. <?php namespace App\Controllers;
  2.  
  3. use CodeIgniter\Controller;
  4.  
  5. class Helloworld extends Controller {
  6.  
  7. }

This is not valid:

  1. <?php namespace App\Controllers;
  2.  
  3. use CodeIgniter\Controller;
  4.  
  5. class helloworld extends Controller {
  6.  
  7. }

This is not valid:

  1. <?php namespace App\Controllers;
  2.  
  3. use CodeIgniter\Controller;
  4.  
  5. class HelloWorld extends Controller {
  6.  
  7. }

Also, always make sure your controller extends the parent controllerclass so that it can inherit all its methods.

Methods

In the above example, the method name is index(). The “index” methodis always loaded by default if the second segment of the URI isempty. Another way to show your “Hello World” message would be this:

  1. example.com/index.php/helloworld/index/

The second segment of the URI determines which method in thecontroller gets called.

Let’s try it. Add a new method to your controller:

  1. <?php namespace App\Controllers;
  2.  
  3. use CodeIgniter\Controller;
  4.  
  5. class Helloworld extends Controller
  6. {
  7.  
  8. public function index()
  9. {
  10. echo 'Hello World!';
  11. }
  12.  
  13. public function comment()
  14. {
  15. echo 'I am not flat!';
  16. }
  17. }

Now load the following URL to see the comment method:

  1. example.com/index.php/helloworld/comment/

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 yourmethod as parameters.

For example, let’s say you have a URI like this:

  1. example.com/index.php/products/shoes/sandals/123

Your method will be passed URI segments 3 and 4 (“sandals” and “123”):

  1. <?php namespace App\Controllers;
  2.  
  3. use CodeIgniter\Controller;
  4.  
  5. class Products extends Controller
  6. {
  7.  
  8. public function shoes($sandals, $id)
  9. {
  10. echo $sandals;
  11. echo $id;
  12. }
  13. }

Important

If you are using the URI Routingfeature, the segments passed to your method will be the re-routedones.

Defining a Default Controller

CodeIgniter can be told to load a default controller when a URI is notpresent, as will be the case when only your site root URL is requested. Let’s try itwith the Helloworld controller.

To specify a default controller open your app/Config/Routes.phpfile and set this variable:

  1. $routes->setDefaultController('Helloworld');

Where ‘Helloworld’ is the name of the controller class you want to be used.

A few lines further down Routes.php in the “Route Definitions” section comment out the line:

  1. $routes->get('/', 'Home::index');

If you now browse to your site without specifying any URI segments you’llsee the “Hello World” message.

Note

The line $routes->get('/', 'Home::index'); is an optimization that you will want to use in a “real-world” app. But for demonstration purposes we don’t want to use that feature. $routes->get() is explained in URI Routing

For more information, please refer to the “Routes Configuration Options” section of theURI Routing documentation.

Remapping Method Calls

As noted above, the second segment of the URI typically determines whichmethod in the controller gets called. CodeIgniter permits you to overridethis behavior through the use of the _remap() method:

  1. public function _remap()
  2. {
  3. // Some code here...
  4. }

Important

If your controller contains a method named _remap(),it will always get called regardless of what your URI contains. Itoverrides the normal behavior in which the URI determines which methodis called, allowing you to define your own method routing rules.

The overridden method call (typically the second segment of the URI) willbe passed as a parameter to the _remap() method:

  1. public function _remap($method)
  2. {
  3. if ($method === 'some_method')
  4. {
  5. $this->$method();
  6. }
  7. else
  8. {
  9. $this->default_method();
  10. }
  11. }

Any extra segments after the method name are passed into _remap(). These parameters can be passed to the methodto emulate CodeIgniter’s default behavior.

Example:

  1. public function _remap($method, ...$params)
  2. {
  3. $method = 'process_'.$method;
  4. if (method_exists($this, $method))
  5. {
  6. return $this->$method(...$params);
  7. }
  8. throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
  9. }

Private methods

In some cases, you may want certain methods hidden from public access.To achieve this, simply declare the method as private or protected.That will prevent it from being served by a URL request. For example,if you were to define a method like this for the Helloworld controller:

  1. protected function utility()
  2. {
  3. // some code
  4. }

then trying to access it using the following URL will not work:

  1. example.com/index.php/helloworld/utility/

Organizing Your Controllers into Sub-directories

If you are building a large application you might want to hierarchicallyorganize or structure your controllers into sub-directories. CodeIgniterpermits you to do this.

Simply create sub-directories under the main _app/Controllers/_one and place your controller classes within them.

Note

When using this feature the first segment of your URI mustspecify the folder. For example, let’s say you have a controller locatedhere:

  1. app/Controllers/products/Shoes.php

To call the above controller your URI will look something like this:

  1. example.com/index.php/products/shoes/show/123

Each of your sub-directories may contain a default controller which will becalled if the URL contains only the sub-directory. Simply put a controllerin there that matches the name of your ‘defaultcontroller’ as specified inyour _app/Config/Routes.php file.

CodeIgniter also permits you to remap your URIs using its URI Routing feature.

Included Properties

Every controller you create should extend CodeIgniter\Controller class.This class provides several features that are available to all of your controllers.

Request Object

The application’s main Request Instance is always availableas a class property, $this->request.

Response Object

The application’s main Response Instance is always availableas a class property, $this->response.

Logger Object

An instance of the Logger class is available as a class property,$this->logger.

forceHTTPS

A convenience method for forcing a method to be accessed via HTTPS is available within allcontrollers:

  1. if (! $this->request->isSecure())
  2. {
  3. $this->forceHTTPS();
  4. }

By default, and in modern browsers that support the HTTP Strict Transport Security header, thiscall should force the browser to convert non-HTTPS calls to HTTPS calls for one year. You canmodify this by passing the duration (in seconds) as the first parameter:

  1. if (! $this->request->isSecure())
  2. {
  3. $this->forceHTTPS(31536000); // one year
  4. }

Note

A number of time-based constants are always available for you to use, including YEAR, MONTH, and more.

helpers

You can define an array of helper files as a class property. Whenever the controller is loadedthese helper files will be automatically loaded into memory so that you can use their methods anywhereinside the controller:

  1. namespace App\Controllers;
  2. use CodeIgniter\Controller;
  3.  
  4. class MyController extends Controller
  5. {
  6. protected $helpers = ['url', 'form'];
  7. }

Validating data

To simplify data checking, the controller also provides the convenience method validate().The method accepts an array of rules in the first parameter,and in the optional second parameter, an array of custom error messages to displayif the items are not valid. Internally, this uses the controller’s$this->request instance to get the data to be validated.The Validation Library docs have details onrule and message array formats, as well as available rules.:

  1. public function updateUser(int $userID)
  2. {
  3. if (! $this->validate([
  4. 'email' => "required|is_unique[users.email,id,{$userID}]",
  5. 'name' => 'required|alpha_numeric_spaces'
  6. ]))
  7. {
  8. return view('users/update', [
  9. 'errors' => $this->validator->getErrors()
  10. ]);
  11. }
  12.  
  13. // do something here if successful...
  14. }

If you find it simpler to keep the rules in the configuration file, you can replacethe $rules array with the name of the group as defined in Config\Validation.php:

  1. public function updateUser(int $userID)
  2. {
  3. if (! $this->validate('userRules'))
  4. {
  5. return view('users/update', [
  6. 'errors' => $this->validator->getErrors()
  7. ]);
  8. }
  9.  
  10. // do something here if successful...
  11. }

Note

Validation can also be handled automatically in the model, but sometimes it’s easier to do it in the controller. Where is up to you.

That’s it!

That, in a nutshell, is all there is to know about controllers.