What are Routes?

Routes are responsible for responding to URL requests. Routing matches url to predefined routes


URI Routing

URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:

example.com/class/function/id/

Setting your own routing rules

See the controller first then will will set our routes


application/controllers/Catalog.php 

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Catalog extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        //code will be here 
    }

    public function product_with_one_param($id) {
        //code will be here 
    }

    public function product_with_two_param($id, $title) {
        //code will be here 
    }

}


Lets now understand routing rules are defined in your application/config/routes.php file.

<?php
//Catalog is the Controller name have index method
$route['journals'] = 'Catalog';// you can access like this http://ci_project/journals

// (:any) will match a segment containing any character
$route['product/(:any)'] = 'catalog/product_with_one_param/$1';

//(:num) will match a segment containing only numbers
$route['product/(:any)/(:any)'] = 'catalog/product_with_two_param/$1/$2'; // you can increase as you want $1 $2 $3....
?>

  • (:any) will match a segment containing any character
  • (:num) will match a segment containing only numbers

Reserved Routes

There are three reserved routes:

//Default Controller Create your own controller and you can replace welcome to ...
$route['default_controller'] = 'welcome';

// page not found you can define your own controller & custom 404 page
$route['404_override'] = ''; 

//allow uri_dashes
$route['translate_uri_dashes'] = FALSE;