What is a Controller?
A Controller receives the user input 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.
Controller Example – CodeIgniter 3
<?php
class Home extends CI_Controller {
public function index() {
echo "Welcome to CodeIgniter 3";
}
}
?>Run in Browser:
http://localhost/project/index.php/home/
Controller Example – CodeIgniter 4
<?php
namespace App\Controllers;
class Home extends BaseController {
public function index() {
echo "Welcome to CodeIgniter 4";
}
}
?>Run the following command in cmd/terminal from the project’s root directory:
> php spark serve
http://localhost:8080Run in Browser:
http://localhost:8080/index.php/Home/index
In the above example, Home.php is a default controller. You can alter the default namespace, controller, and method from /app/Config/Routes.php.
