Controllers in CodeIgniter 4

Controllers in CodeIgniter 4

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

A Controller is a class file, which contains collection of methods and properties to handle HTTP Requests. The name of the controller class will always associated with URI segments, We know that, the first segment of an URI is always controller class name

let’s create a controller

<?php namespace App\Controllers;

use CodeIgniter\Controller;

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

All the Controller classes should be saved in app/Controllers folder.

The file must be called ‘ Helloworld.php ‘, with a capital ‘H’. Controller class names MUST start with an uppercase letter and ONLY the first character can be uppercase.

Now visit your site using a URL similar to this:

You can see the output ” Hello World ”

Every Controller should  extends the parent controller class so that it can inherit all its methods. By extending Controller class, it will provides several features that are available to all of your controllers. 

Request Object ( $this->request )

Response Object ( $this->response )

Logged Object ( $this->logger )

Helpers, validations and more…

Share this post