For any queries you can reach us at infovistarindia@gmail.com / WhatsApp us: +919158876092

Controllers in CodeIgniter 4

What is Controller?

A Controller receives the user input and validates it, and then passes the input to the Model. It performs interaction on the model objects.

It is a simple class file. The name of the class is associated with URI.

The first letter of a class name must be capital.

CodeIgniter 3 Example

<?php 
class Home extends CI_Controller {
	
	public function index() {
		echo "Welcome to CodeIgniter 3";
	}

}
?>

Run in URL:

http://localhost/proejct/index.php/home/

CodeIgniter 4 Example

<?php
namespace App\Controllers;		

class Home extends BaseController {
	
	public function index() {
		echo "Welcome to CodeIgniter 4";
	}

}
?>

Run following command in cmd/terminal from project’s root directory:

> php spark serve
    http://localhost:8080

Run in URL

http://localhost:8080/index.php/Home/index

In above example,

Home.php is a default controller. You can alter the default namespace, controller and method from /app/Config/Routes.php.