×

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to access the controller and its functions on the browser. It contains one to one mapping with the controller and its actions in the URL, that follows the following pattern:

localhost/controller_name/action_name/

For example, example.com/main/about-us

Where

  • example.com is the domain name
  • main is the controller name that you have passed in the routes.php.
  • about-us is the action name of the controller that calls on the browser.

If the particular request is not full filled by the routes in the URL, the CodeIgniter throws an error, page not found.

Routing path in the CodeIgniter

The routes.php file is defined inside the config folder of the application in CodeIgniter. When we run application, the routes file performs three actions, by default that calls on the browser.

$route['default_controller']
= 'welcome';
 $route['404_override'] = '';
 $route['translate_uri_dashes'] = FALSE; 

Here, the $route['default_controller'] represents that you had created a default controller in the routes.php file. It automatically calls when you run application in the browser.

The $route[‘404_override’] represents that it will throws an exception message or page not found in the application. Also, you can show an exception related page in your application when the particular request does not present. 

$route[‘ translate_uri_dashes’] represents that it is not a route. It contains a Boolean value that allows you to automatically convert dashes (-) into underscores in the controller and action/method.

So, when you run CodeIgniter application in the browser, it fetches a welcome controller from the routes.php file, by default. Because it has a predefined welcome controller to run in the server.

Example: We are going to create a controller and action that can call in URL. So, you have to define controller and action in routes.php like: $route[ ‘hello’ ] = ’Users/main’;

We have created a Users.php in application/controller folder.

Users.php

<?php
 class Users extends CI_Controller {
  public function index()
 {
  echo “ This is the first program in which, we have define routes”;   
 }
  public function main()
     {    
         echo "Hello Friends, CodeIgniter is a PHP framework";
         echo "<br>";
         echo "Here, We have used route['(:num)']= 'Users/main' which accept only number in url to call users/main";
     }
 }
 ?> 

Let’s execute the above program. In the browser, type the URL localhost/CodeIgniter-3.1.11/index.php/hello. It returns the output, as shown below.

CodeIgniter Routes

We can also customize the routes in two ways:

  • Wildcards
  • Regular

Wildcards

The wildcards route might be represented like the following:

$route[ ‘hello/ :num’ ] = ‘Users/main’ ;

The $route[] contains an array key that is matched with the URL to select a particular controller and action. In the above example, hello is the first segment of the URL, and the num is the second argument that calls the controller. It calls the action name instead of calling the full name in the URL.

The wildcards are divided into two parts:

  1. :num - The :num function is used to match only numbers in routes that passes from the URL.

$route[ ‘hello/ :num’ ] = ‘Users/main’ ;

Example: We are going to create a controller and action that can call in URL. So, you have to define controller and action in routes.php such as $route[ ‘hello/:num’ ] = ’Users/main’;

Users.php

<?php
 class Users extends CI_Controller {
  public function index()
 {
  echo “ This is the first program in which, we have define routes”;   
 }
  public function main()
     {    
         echo "Hello Friends, CodeIgniter is a PHP framework";
         echo "<br>";
        echo " we have passed <b>'hello/num'</b> in route like (route['hello/:num'] = 'Users/main') to direct access the users/main. you can define any number 0-9, 123, etc.";
     }
 }
 ?> 

Run the above program by invoking the URL localhost/CodeIgniter-3.1.11/index.php/ and write 123. It returns the output, as shown below.

CodeIgniter Routes

$route[ ‘( :num)’ ] = ‘Users/main’ ;

Similarly, you can also define the above routes method in routes.php for getting the following output.

CodeIgniter Routes

Note: When you execute the program then it takes only a number for calling the controller (Users) and its method (main).

2. :any – The :any function is used to accept or match any type of character, either the number or string from the URL to match with define segments in routes.

Syntax: $route[‘ (:any) ‘] = ‘MainController/index/$1’ ;

Example: You have to create controller and action, that can call in URL, as you have done in the :num function. So, you have to define controller and action in routes.php.

$route[ ‘users/:any’ ] = ’Users/main’;

Users.php

<?php
 class Users extends CI_Controller {
  public function index()
 { 
  echo “ This is the first program in which, we have define routes”;   
 }
 public function main()
     {  
         echo "Hello Friends, CodeIgniter is a PHP framework";
         echo "<title> Welcome to Tutorials And Examples </title>";
         echo "<br>";
         echo " Here, we have described about the <b> (:any) </b> function in routes" ; 
         echo "<br>";
         echo " <b> routes(:num) ='Users/main <b>" ;    
     }
 }
 ?> 

Let’s run the application. Open the browser and invoke the URL localhost/CodeIgniter-3.1.11/index.php/users/hello. It returns the output, as shown below.

CodeIgniter Routes

Note: The condition should be matched, as you have defined in routes.php file such as:

$route[users/:any]
= ‘USERS/main’
 $route[users/:any/:num] = ‘USERS/main’ 

Similarly, define the function “$routes (users/: any/: any) = ‘Users/main’” in routes.php file. Execute the URL localhost/CodeIgniter-3.1.11/index.php/users/hello/boy. It shows the following output.

CodeIgniter Routes

Note: You can specify any alphanumeric string in the URL,as shown above. You have to follow the same sequence as defined in routes.php file. Or you can define the $route[] with the combination of “:any/:num”, that shows the output, as shown below.

CodeIgniter Routes

Regular Expressions

It is similar to wildcards. You can define regular expression in $routes array to match the expression that passes from the URL. If the matches found with the $routes array, it will fetch that page from the routes to show on the browser.

Syntax:

$route['usesrs/([a-z]+)/(\d+)'] = '$1/id_$2';=
‘product/parts’;

URL Suffix

If you want to add a suffix to show the extension of the file in url, go to config.php file in application/config folder and add the extension as shown below. In this method, you can add any type of extension such as .css, .php, .html, and .jsp, etc.

CodeIgniter Routes

Example: Consider the URL https:// localhost/CodeIgniter-3.1.11/index.php/hello

After adding .html suffix in the URL, it looks like https:// localhost/CodeIgniter-3.1.11/index.php/hello.html, andshows the following output.

CodeIgniter Routes

Passing Parameters from URL to controller

In this method of routes, you can pass the parameters from URL to the controller. For passing parameters, define the argument in controller’s action along with the routes.php file. For example:

$route[' tutor/ (:num)/ (:any)'] = 'Users/main/$1/$2'; 

Here, In the above function we are passing the parameter in the URL. We have used two parameters in the $route[] function that is :num and :any. The $1 represent the :num and $2 represents :any. The first parameter ($1) can accept only numeric value while in the second parameter ($2) you can pass either numeric or string value.

Users.php

<?php
 class Users extends CI_Controller {
 public function main($id, $name)
     { 
         echo "Hello Friends, CodeIgniter is a PHP framework";
         echo "<title> Welcome to Tutorials And Examples </title>"; 
      echo "<br>"; 
        echo "Enter your number" .' ' .$id. ' '. "<br>". "Tutorial name" .' ' .$name;
     }
 }
 ?> 

Now execute the above program. Open the browser and invoke the URL localhost/CodeIgniter/index.php/tutor/ and pass the parameters values 20 and CodeIgniter, respectively. It shows the output, as shown below.

CodeIgniter Routes

Related Topics

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

CodeIgniter URL Helper

The helper file contains some predefined functions that are used to work with URL, such as generate the current file path, transfer control, redirect, create a link, etc. Load the URL Helper Before using...

7 minutes read.

CodeIgniter Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

4 minutes read.

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

3 minutes read.

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 minutes read.

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 minutes read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

2 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 minutes read.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 minutes read.