Controllers
A controller is a straightforward class document. As the name proposes, it controls the entire application by URI.
Creating a Controller
To begin with, go to application/controllers organizer. You will discover two records there, index.html and Welcome.php. These records accompany the CodeIgniter.
Keep these documents as they seem to be. Make another record under a similar way named "Test.php". Compose the accompanying code in that document −
<?php
class Test extends CI_Controller {
public function index() {
echo "Hello World!";
}
}
?>
The Test class expands an in-manufactured class called CI_Controller. This class must be stretched out at whatever point you need to make your own Controller class.
Calling a Controller
The above controller can be called by URI as follows −
http://www.your-domain.com/index.php/test
Notice "test" in the above URI after index.php. This demonstrates the class name of controller. As we have given the name of the controller "Test", we are stating "test" after the index.php. The class name must beginning with capitalized letter however we have to compose lowercase letter when we call that controller by URI. The general language structure for calling the controller is as per the following −
http://www.your-domain.com/index.php/controller/method-name
Creating & Calling Constructor Method
Let us change the above class and make another technique named "hi".
<?php
class Test extends CI_Controller {
public function index() {
echo "This is default function.";
}
public function hello() {
echo "This is hello function.";
}
}
?>
We can execute the above controller in the accompanying three different ways −
- http://www.your-domain.com/index.php/test
- http://www.your-domain.com/index.php/test/list
- http://www.your-domain.com/index.php/test/hi
In the wake of visiting the main URI in the program, we get the yield as appeared in the image given beneath. As should be obvious, we got the yield of the technique "list", despite the fact that we didn't pass the name of the strategy the URI. We have utilized just controller name in the URI. In such circumstances, the CodeIgniter calls the default strategy "record".
Visiting the second URI in the program, we get a similar yield as appeared in the above picture. Here, we have passed strategy's name after controller's name in the URI. As the name of the strategy seems to be "file", we are getting a similar yield.
Visiting the third URI in the program, we get the yield as appeared in picture given beneath. As should be obvious, we are getting the yield of the strategy "hi" since we have passed "hi" as the technique name, after the name of the controller "test" in the URI.
Points to Remember
- The name of the controller class must beginning with a capitalized letter.
- The controller must be called with lowercase letter.
- Try not to utilize a similar name of the strategy as your parent class, as it will supersede parent class' usefulness.
Views
This can be a straightforward or complex website page, which can be called by the controller. The page may contain header, footer, sidebar and so forth. View can't be called legitimately. Let us make a straightforward view. Make another document under application/sees with name "test.php" and duplicate the beneath given code in that record.
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>CodeIgniter View Example</title>
</head>
<body>
CodeIgniter View Example
</body>
</html>
Change the code of use/controllers/test.php document as appeared in the underneath.
Stacking the View
The view can be stacked by the accompanying linguistic structure −
$this->load->view('name');
Where name is the view document, which is being rendered. In the event that you have intended to store the view record in some registry, at that point you can utilize the accompanying sentence structure −
$this->load->view('directory-name/name');
It isn't important to determine the expansion as php, except if some different option from .php is utilized.
The file() technique is calling the view strategy and passing the "test" as contention to see() technique since we have put away the html coding in "test.php" record under application/sees/test.php.
<?php
class Test extends CI_Controller {
public function index() {
$this->load->view('test');
}
}
?>
Here is the yield of the above code −
The accompanying flowchart delineates of how everything functions −
Models
Models classes are intended to work with data in the database. For instance, on the off chance that you are utilizing CodeIgniter to oversee clients in your application, at that point you should have model class, which contains capacities to embed, erase, refresh and recover your clients' information.
Creating Model Class
Model classes are put away in application/models registry. Following code tells the best way to make model class in CodeIgniter.
<?php
Class Model_name extends CI_Model {
Public function __construct() {
parent::__construct();
}
}
?>
Where Model_name is the name of the model class that you need to give. Each model class must acquire the CodeIgniter's CI_Model class. The principal letter of the model class must be in capital letter. Following is the code for clients' model class.
<?php
Class User_model extends CI_Model {
Public function __construct() {
parent::__construct();
}
}
?>
The above model class must be spared as User_model.php. The class name and document name must be same.
Loading Model
Model can be brought in controller. Following code can be utilized to stack any model.
$this->load->model('model_name');
Where model_name is the name of the model to be stacked. In the wake of stacking the model you can just call its technique as demonstrated as follows.
$this->model_name->method();
Auto-loading Models
There might be circumstances where you need some model class all through your application. In such circumstances, it is better on the off chance that we autoload it.
/*
| ---------------------------------------------------------------
| 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();
As appeared in the above figure, pass the name of the model in the cluster that you need to autoload and it will be autoloaded, while framework is in introduction state and is open all through the application.
Helpers
As the name proposes, it will assist you with building your framework. It is separated into little capacities to serve diverse usefulness. Various aides are accessible in CodeIgniter, which are recorded in the table underneath. We can assemble our own partners as well.
Aides are commonly put away in your framework/assistants, or application/aides index. Custom assistants are put away in application/partners catalog and frameworks' aides are put away in framework/aides index. CodeIgniter will glance first in your application/assistants index. On the off chance that the catalog doesn't exist or the predetermined aide isn't found, CodeIgniter will rather, look in your worldwide framework/assistants/registry. Every assistant, regardless of whether it is custom or framework partner, must be stacked before utilizing it.
Given below are the most commonly used Helpers.
S.N. Helper Name and Description
1
Exhibit Helper
The Array Helper record contains capacities that help with working with exhibits.
2
CAPTCHA Helper
The CAPTCHA Helper record contains capacities that help with making CAPTCHA pictures.
3
Treat Helper
The Cookie Helper record contains capacities that help with working with treats.
4
Date Helper
The Date Helper document contains capacities that assist you with working with dates.
5
Catalog Helper
The Directory Helper document contains capacities that help with working with registries.
6
Download Helper
The Download Helper lets you download information to your work area.
7
Email Helper
The Email Helper gives some assistive capacities to working with Email. For a progressively vigorous email arrangement, see CodeIgniter's Email Class.
8
Document Helper
The File Helper document contains capacities that help with working with records.
9
Structure Helper
The Form Helper document contains capacities that help with working with structures.
10
HTML Helper
The HTML Helper record contains capacities that help with working with HTML.
11
Inflector Helper
The Inflector Helper document contains capacities that licenses you to change words to plural, solitary, camel case, and so on.
12
Language Helper
The Language Helper document contains capacities that help with working with language records.
13
Number Helper
The Number Helper record contains capacities that assist you with working with numeric information.
14
Way Helper
The Path Helper record contains capacities that licenses you to work with document ways on the server.
15
Security Helper
The Security Helper document contains security related capacities.
16
Smiley Helper
The Smiley Helper document contains capacities that let you oversee smileys (emojis).
17
String Helper
The String Helper record contains capacities that help with working with strings.
18
Content Helper
The Text Helper document contains capacities that help with working with content.
19
Typography Helper
The Typography Helper record contains capacities that help your arrangement message in semantically important manners.
20
URL Helper
The URL Helper document contains capacities that help with working with URLs.
21
XML Helper
The XML Helper record contains capacities that help with working with XML information.
Loading a Helper
An assistant can be stacked as appeared underneath −
$this->load->helper('name');
Where name is the name of the partner. For instance, on the off chance that you need to stack the URL Helper, at that point it tends to be loade
$this->load->helper('url');
Routing
CodeIgniter has easy to understand URI steering framework, with the goal that you can without much of a stretch re-course URL. Regularly, there is a coordinated connection between a URL string and its relating controller class/strategy. The sections in a URI regularly follow this example −
your-domain.com/class/method/id/
- The primary portion speaks to the controller class that ought to be conjured.
- The subsequent fragment speaks to the class capacity, or strategy, that ought to be called.
- The third, and any extra portions, speak to the ID and any factors that will be passed to the controller.
In certain circumstances, you might need to change this default steering instrument. CodeIgniter gives office through which you can set your own steering rules.
Customize Routing Rules
There is a specific record where you can deal with all these. The record is situated at application/config/routes.php. You will discover a cluster called $route in which you can alter your steering rules. The key in the $route cluster will choose what to course and the worth will choose where to course. There are three held courses in CodeIgniter.
S.N. Reserved Routes and Description
1
$route['default_controller']
This course shows which controller class ought to be stacked, if the URI contains no information, which will be the situation when individuals load your root URL. You are urged to have a default course in any case a 404 page will show up, naturally. We can set landing page of site here so it will be stacked as a matter of course.
2
$route['404_override']
This course demonstrates which controller class ought to be stacked if the mentioned controller isn't found. It will supersede the default 404 blunder page. It won't influence to the show_404() work, which will keep stacking the default error_404.php document in application/sees/blunders/error_404.php.
3
$route['translate_uri_dashes']
As clear by the Boolean worth, this isn't actually a course. This choice empowers you to naturally supplant runs ('- ') with underscores in the controller and strategy URI portions, along these lines sparing you extra course passages on the off chance that you have to do that. This is required in light of the fact that the scramble is certainly not a substantial class or strategy name character and will cause a lethal blunder, in the event that you attempt to utilize it.
Courses can be tweaked by trump cards or by utilizing standard articulations however remember that these altered guidelines for steering must come after the held principles.
Wildcards
We can utilize two trump card characters as clarified beneath −
- (:num) − It will coordinate a portion containing just numbers.
- (:any) − It will coordinate a fragment containing any character.
Example
$route['product/:num']='catalog/product_lookup';
In the above model, if the exacting word "item" is found in the principal fragment of the URL, and a number is found in the subsequent portion, the "inventory" class and the "product_lookup" strategy are utilized.
Regular Expressions
Like trump cards, we can likewise utilize normal articulations in $route exhibit key part. In the event that any URI matches with customary articulation, at that point it will be steered to the worth part set into $route exhibit.
Example
$route['products/([a-z]+)/(\d+)']='$1/id_$2';
In the above model, a URI like items/shoes/123 would rather call the "shoes" controller class and the "id_123" strategy.