Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to route / customize route in CodeIgniter

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.65k
    Comment on it

    In CodeIgniter URI routing system we find a one-to-one relationship between the path Controller_class/Controller_class_method and the URL .All the segments in a URI displays the below pattern :

    Domain_name/Controller_class_name/Controller_class_method/id/name/

    In the above URI:

    The first segment shows that Controller Class has been invoked.

    The second segment shows that the function or method of the above Controller Class is called.

    In third segment or more segments that are divided by the “/” represent the “id” and “name” or any other variable which is passed to the above mentioned Controller Class. We can increase these segments as per requirement.


    In case if we want to change the default route mechanism, we have a facility provided by CodeIgniter to set our own routing mechanism.

    Customization of Routing Rules:

    For customizing the routing rules in CodeIgniter we have to go to the file routes.php which we can locate at the below-mentioned path:

    application/config/routes.php

    We can find an array “$route” which will help us to write our customize rules for routing through the site. In an array, we find the array key that represents what to route and the value of the key will indicate where we have to route.

    CodeIgniter consists of 3 reserved routes mentioned below:

     

    1)- $route['default_controller'] = “dashboard”

    It displays which Controller_Class has to be loaded.

    If the URI contains only the Controller_Class name generally we have a home page as a default page

     

    2)- $route['404_override'] = '';

    It displays when Controller_Class that has to be loaded is not found in the requested URI

    Here we can display an error_404.php page for the function error_404 in “Controller_Errors” class. By default the show_404 method is called.

     

    3)- $route['translate_uri_dashes'] = FALSE;

    The Boolean value indicates that this is not exactly a route. This option will automatically place underscores in place of dashes ('-') in the controller and method URI segments.This is useful as the dash is not a valid Controller_Class or method-name character which will cause a fatal error.

    We can also use wildcards or regular expressions for customizing routes.

    Note: We place customized rules below the reserved rules.

    Wildcards

    We can use two wildcard characters as explained below −

    (:num) − This will match a segment which contains only numbers.

    (:any) − This will match a segment which contains any character.

     

    Examples:

    In the example below, if the word “profile” is found in the first segment of the URL, and if there is a number in the second segment, the “Controller_User” class and the “profile_display” method will be routed.

    $route['profile/:num']='user/profile_display';

    Regular Expressions

     

    Here we place a regular expression in place of an array key and on the matching of URI with a regular expression, we will be routed to the value part of any array $route.

    In the example below, a URI: user/guest/123 will call the “Controller_Guest” and the “id_123” method.

    $route['user/([a-z]+)/(\d+)'] = '$1/id_$2';

    For more information :https://codeigniter.com/user_guide/general/routing.html

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: