CodeIgniter Route Configuration

Route configuration generally used for remapping URL string. Typically there is a one-to-one relationship between an URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:

example.com/class/function/id/

In some instances, however, you may want to remap this relationship so that a different class/method can be called instead of the one corresponding to the URL.

For example, you have a class for displaying users profile having the class name “user” and function “base_info()”  with user id as following:
example.com/user/base_info
Now you want to remap the URL like this:
example.com/profile/id

Setting your own route configuration rules using following steps:

  1. Go to your application directory then open routes.php file under config directory( application /config / routes.php ).
  2. At end of file find $route array
  3. Specify your own routeing criteria ($route[‘profile/:num’]=‘user/base_info’;)

 Note : Routes can either be specified using wildcards or Regular Expressions.

Wildcards

A typical wildcard route might look something like this:

$route[‘profile/:num’]=‘user/base_info’;

In a route, the array key contains the URI to be matched, while the array value contains the destination it should be re-routed to. In the above example, if the literal word “profile” is found in the first segment of the URL, and a number is found in the second segment, the “user” class and the “base_info” method are instead used.

You can match literal values or you can use two wildcard types these are:

  1. (:num) will match a segment containing only numbers.
  2. (:any) will match a segment containing any character (except for ‘/’, which is the segment delimiter).

There are three reserved routes:

1) $route[‘default_controller’] = ‘welcome’;

This route indicates which controller class should be loaded if the URI contains no data. In the above example, the “welcome” class would be loaded.

2) $route[‘404_override’] = ‘errors/page_missing’;

This route will tell the Router which controller/method to use if those provided in the URL cannot be matched to a valid route.

 3)$route[‘translate_uri_dashes’] = FALSE;

This is not exactly a route but allows you to automatically route controller and method names that contain dashes. ‘-‘ isn’t a valid class or method name character, so it requires translation. When you set this option to TRUE, it will replace ALL dashes in the controller and method URI segments.

 

<<Previous                                                                                           Next>>

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

One Pingback

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.