CodeIgniter Controllers
Controllers take care of moving the data between the model and the views and directing traffic
based on user actions. For example, when a user submits a form, the controller collects the data and
shuffles it off to the model, which should ensure the data is valid. The controller then tells the View
(or JavaScript layer) whether it was successful.
How Controllers Work
Before I explain how, let’s look at the routes.php file. This file of this Application->Config>routes.php.
when we browse http://localhost/codeigniter/, CI will look for the welcome controller and by default call the index method.
If we add an entry in routes.php
1 |
$route['home_page'] = 'welcome/home_page'; |
then browse url http://localhost/codeigniter/home_page, CI will look for a method name home_pagein Welcome controller.
Source code of Welcome controller
1 2 3 4 5 6 7 8 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Welcome extends CI_Controller { public function index() { $this->load->view('welcome_message'); } } |
class Welcome extends CI_Controller: class Welcome extends(inherit) the CI core controller.
public function index() : This function is executed when the home page is browsed.