CodeIgniter - Basic Concepts
Controllers
Creating a Controller
<?php
class Test extends CI_Controller {
public function index() {
echo "Hello World!";
}
}
?>
Calling a Controller
http://www.your-domain.com/index.php/test
http://www.your-domain.com/index.php/controller/method-name
Creating & Calling Constructor Method
<?php
class Test extends CI_Controller {
public function index() {
echo "This is default function.";
}
public function hello() {
echo "This is hello function.";
}
}
?>
- http://www.your-domain.com/index.php/test
- http://www.your-domain.com/index.php/test/index
- http://www.your-domain.com/index.php/test/hello


Points to Remember
- The name of the controller class must start with an uppercase letter.
- The controller must be called with lowercase letter.
- Do not use the same name of the method as your parent class, as it will override parent class’s functionality.
Views
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>CodeIgniter View Example</title>
</head>
<body>
CodeIgniter View Example
</body>
</html>
Loading the View
$this->load->view('name');
$this->load->view('directory-name/name');
<?php
class Test extends CI_Controller {
public function index() {
$this->load->view('test');
}
}
?>


Models
Creating Model Class
<?php
Class Model_name extends CI_Model {
Public function __construct() {
parent::__construct();
}
}
?>
<?php
Class User_model extends CI_Model {
Public function __construct() {
parent::__construct();
}
}
?>
Loading Model
$this->load->model('model_name');
$this->model_name->method();
Auto-loading Models
/*
| ---------------------------------------------------------------
| Auto-Load Models
| ---------------------------------------------------------------
| Prototype:
|
| $autoload['model'] = array('first_model', 'second_model');
|
| You can also supply an alternative model name to be assigned
| in the controller:
|
| $autoload['model'] = array('first_model' => 'first');
*/
$autoload['model'] = array();
Helpers
Loading a Helper
$this->load->helper('name');
$this->load->helper('url');
Routing
your-domain.com/class/method/id/
- The first segment represents the controller class that should be invoked.
- The second segment represents the class function, or method, that should be called.
- The third, and any additional segments, represent the ID and any variables that will be passed to the controller.
Customize Routing Rules
| S.N. | Reserved Routes & Description |
|---|---|
| 1 | $route['default_controller'] This route indicates which controller class should be loaded, if the URI contains no data, which will be the case when people load your root URL. You are encouraged to have a default route otherwise a 404 page will appear, by default. We can set home page of website here so it will be loaded by default. |
| 2 | $route['404_override'] This route indicates which controller class should be loaded if the requested controller is not found. It will override the default 404 error page. It won’t affect to the show_404() function, which will continue loading the default error_404.php file in application/views/errors/error_404.php. |
| 3 | $route['translate_uri_dashes'] As evident by the Boolean value, this is not exactly a route. This option enables you to automatically replace dashes (‘-‘) with underscores in the controller and method URI segments, thus saving you additional route entries if you need to do that. This is required because the dash is not a valid class or method-name character and will cause a fatal error, if you try to use it. |
Wildcards
- (:num) − It will match a segment containing only numbers.
- (:any) − It will match a segment containing any character.
$route['product/:num']='catalog/product_lookup';
Regular Expressions
$route['products/([a-z]+)/(\d+)']='$1/id_$2';