How to route a URL in CodeIgniter

A route is a way to remapping, makes more meaningful and SEO friendly URLs. The visitor views the newly generated page URL and the route calls the defined action URL which will be hidden from the visitors.

In CodeIgniter, a URL consists of controller-name/method-name but you can manipulate it by declaring routing.

All routed actions will be written on the specific file.

How to route a URL in CodeIgniter


Contents

  1. Download
  2. Controller
  3. Route
  4. Wildcard
  5. Conclusion

1. Download

  • Download the Codeigniter from here.
  • Extract zip file on the htdocs folder and rename the folder e.g. codeigniter.
  • Open application/config/config.php file.
  • Update the $config['base_url'] value.
$config['base_url'] = 'http://localhost/codeigniter/';

2. Controller

I am defining some of the methods that use while defining the route.

  • Navigate to application/controllers/ directory.
  • Open the Welcome.php controller file.
  • Define some methods – index(), admin(), showList(), updateList(), deleteList(). I created Links() methods to display the links.

Completed Code

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
 
 public function index() { 
   echo "index"; 
   self::Links(); 
 }

 public function showList(){ 
   echo "show List"; 
   self::Links(); 
 }
 
 public function admin(){ 
   echo "admin"; 
   self::Links(); 
 }
 
 public function deleteList($delid=0){ 
   echo "Delete List : ".$delid; 
   self::Links(); 
 }
 
 public function updateList($name="",$updateid = 0){ 
   echo "name : ".$name; 
   echo "update id : ".$updateid."<br>";
   self::Links();  
 }
 
 public function Links(){ 
   $this->load->helper('url'); 
   echo "<br><br> 
       <a href='".base_url()."'>Index</a><br> 
       <a href='".base_url()."index.php/welcome/list'>Show</a><br> 
       <a href='".base_url()."index.php/admin'>Admin</a><br> 
       <a href='".base_url()."index.php/welcome/delete/12'>Delete</a><br> 
       <a href='".base_url()."index.php/welcome/update/sonarika/3'>Update</a><br> "; 
 }

}

Above created controller methods normally access like – localhost/codeigniter/index.php/welcome/showList where it consists of index.php/controller-name/method-name but I will manipulate it by defining routing in route.php file.


3. Route

All routing is defined in route.php file.

  • Navigate to your project.
  • Open application/config/routes.php file.
$route['welcome/list'] = 'welcome/showList/';
$route['admin'] = 'welcome/admin/';

I have defined two values in $route array variable which consist of the key and a value. The key is matched in the URL if is found then re-route to the value.

  1. I changed the displayed method name from showList to list. Now the URL to access is – localhost/codeigniter/index.php/welcome/list. NOTE – There is no list name method is been declared in Welcome.php controller file.
  2. I defined a single key admin which does not consist of controller-name/method-name pattern and it is pointing to welcome/admin/. Now the access URL is localhost/codeigniter/index.php/admin/ .

4. Wildcard

It is mainly used when creating a URL related to the parameter.

There are two types of wildcards –

  • :num – match number on the URL if its find any other type of values like – string then it will show 404 Page not found error.
  • : any – match any type of value either string or number.
$route['welcome/delete/(:num)'] = 'welcome/deleteList/$1';
$route['welcome/update/(:any)/(:num)'] = 'welcome/updateList/$1/$2';

Defined two routes –

  1. I used :num wildcard in Array key and on value-added $1 to access the value in the controller.
  2. Used :any and :num wildcard in Array key and on value-added $1/$2 for access to the controller.

5. Conclusion

With routing, you can either hide controller-name, method-name, or both of them and display the new URL to the visitor.

Use the wildcard rules according to your requirements while defining the routing.

If you found this tutorial helpful then don't forget to share.

Leave a Comment