what is routes?

How to create routes in codeigniter

CodeIgniter has user-friendly URI routing system.

there is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:


https://ittutorial.in/controller-class/controller-method/arguments/

ittutorial.in/class/function/parameters/

  • The first segment represents the controller class that should be invoked.
  • The second segment represents the class function, or method, that should be called.
  • The third, and any additional segments, represent the ID and any variables that will be passed to the controller function.
  • Note :- In some situations, you may want to change this default routing mechanism. CodeIgniter provides facility through which you can set your own routing rules.

    Routing rules.

    Routing rules are defined in your application/config/routes.php file. You will find an array called $route in which you can customize your routing rules. The key in the $route array will decide what to route and the value will decide where to route. There are three reserved routes in CodeIgniter.

  • $route['default_controller'] :- This route indicates which controller class should be loaded, if the URI contains no data, which will be the case when people load your root URL. You are encouraged to have a default route otherwise a 404 page will appear, by default. We can set home page of website here so it will be loaded by default.
  • $route['404_override'] :- This route indicates which controller class should be loaded if the requested controller is not found. It will override the default 404 error page. It won’t affect to the show_404() function, which will continue loading the default error_404.php file in application/views/errors/error_404.php.
  • $route['translate_uri_dashes'] :- As evident by the Boolean value, this is not exactly a route. This option enables you to automatically replace dashes (‘-‘) with underscores in the controller and method URI segments, thus saving you additional route entries if you need to do that. This is required because the dash is not a valid class or method-name character and will cause a fatal error, if you try to use it.

  • Routes can be customized by wildcards or by using regular expressions but keep in mind that these customized rules for routing must come after the reserved rules.

    Wildcards

    We can use two wildcard characters as explained below −

  • (:num) − match a segment containing only numbers.
  • (:any) − match a segment containing any character.
  • Example

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


    Note : Here 'home' is the controller class Name.

    Example

    Open the routing file located at application/config/routes.php and add the following two lines. Remove all other code that sets any element in the $route array.

    $route['default_controller'] = 'login/view';
    $route['(:any)'] = 'pages/view/$1';

    CodeIgniter reads its routing rules from top to bottom and routes the request to the first matching rule. Each rule is a regular expression (left-side) mapped to a controller and method name separated by slashes (right-side). When a request comes in, CodeIgniter looks for the first match, and calls the appropriate controller and method, possibly with arguments.

    Example

    $route['default_controller'] = 'home';
    $route['admin'] = 'admin/login';
    $route['404_override'] = '';
    $route['translate_uri_dashes'] = FALSE;
    $route['login/(:any)']= 'admin/login';

    Here admin is the directory created under the controller. and login is the controller name under the admin directory.

    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 "product" is found in the first segment of the URL, and a number is found in the second segment, the "catalog" class and the "product_lookup" method are instead used.

    Note : You can match literal values or you can use two wildcard types:
    (:num) will match a segment containing only numbers. (:any) will match a segment containing any character (except for ‘/’, which is the segment delimiter).
    Wildcards are actually aliases for regular expressions, with :any being translated to [^/]+ and :num to [0-9]+, respectively. Note
    Routes will run in the order they are defined. Higher routes will always take precedence over lower ones. Note
    Route rules are not filters! Setting a rule of e.g. ‘foo/bar/(:num)’ will not prevent controller Foo and method bar to be called with a non-numeric value if that is a valid route.

    Examples
    Here are a few routing examples:

    $route['blog'] = 'Myblogs';
    A URL containing the word “blog” in the first segment will be remapped to the “Myblogs” class.

    $route['blog/news'] = 'Myblogs/users/07';
    A URL containing the segments blog/news will be remapped to the “Myblogs” class and the “users” method. The ID will be set to “07”.

    $route['product/(:any)'] = 'catalog/product_view';
    A URL with "product" as the first segment, it is the sub directory and anything in the second will be remapped to the "catalog" class and the "product_view" method.

    $route['product/(:num)'] = 'catalog/product_view_by_id/$1';
    A URL with “product” as the first segment and it the directory, and a number in the second will be remapped to the “catalog” class and the “product_view_by_id” method passing in the match as a variable to the method.

    Regular Expressions

    $route['products/([a-z]+)/(\d+)']='$1/id_$2'; In the above example, a URI similar to products/shoes/123 would instead call the “products” controller class and the “shoes” method.


    Reserved Routes

    There are three reserved routes:

  • $route['default_controller'] = 'welcome';
    This route points to the action that should be executed if the URI contains no data, which will be the case when people load your root URL. The setting accepts a controller/method value and index() would be the default method if you don’t specify one. In the above example, it is Welcome::index() that would be called.
  • $route['404_override'] = '';
    This route indicates which controller class should be loaded if the requested controller is not found. It will override the default 404 error page. Same per-directory rules as with ‘default_controller’ apply here as well. It won’t affect to the show_404() function, which will continue loading the default error_404.php file at application/views/errors/error_404.php.
  • $route['translate_uri_dashes'] = FALSE;
    this is not exactly a route. This option enables you to automatically replace dashes (‘-‘) with underscores in the controller and method URI segments, thus saving you additional route entries if you need to do that. This is required, because the dash isn’t a valid class or method name character and would cause a fatal error if you try to use it.
  • Note :- The reserved routes must come before any wildcard or regular expression routes.

    URL SEO Friendly in codigniter

    Removing index.php from url using .htaccess

    RewriteEngine on
    RewriteCond $1! ^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]

    Default controller Change

    $route['default_controller'] = 'welcome';
    change to
    $route['default_controller'] = 'your_controller_name'

    URI Routing

    // it hide the controller name from url. $route['default_controller'] = "home";
    $route['aboutus'] = "home/about";
    $route['service'] = "home/service";
    $route['contactus'] = "home/contact";



    you write the link your on view page.such as
    <a href="<?php echo base_url()?>aboutus"></a>
    <a href="<?php echo base_url()?>contactus"></a>

    it redirect to home/about controller

    Dynamic URL's Route


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


    $route['news'] = "category/news/1";

    $route['bollywood'] = "category/bollywood/2";

    Dynamic Product URL

    Example category url

    trendeelook.com/mens/t-shirt/
    trendeelook.com/mens/shirt/
    trendeelook.com/mens/trousers/

    rounting code would be

    $route['mens-clothing/(:any)'] = "mens/clothing/$1";

    Example product urls

    trendeelook.com/mens-fit-shirt-sale/
    trendeelook.com/woman-fit-shirt-sale/
    trendeelook.com/kids-fit-shirt-sale/

    rounding code should be


    $route['default_controller'] = "product";


    $route['(:any)'] = "product/index/$1";

    Adding a URL Suffix

    $config['url_suffix'] = '.html';

    Routing Example

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

    $route['admin'] = 'admin/login';
    //$route['login/(:any)']= 'admin/login';

    $route['news/(:any)']= 'news/postdetails/$1';
    $route['post/(:any)']= 'post/postdetails/$1';
    $route['bollywood']= 'category/bollywood/$1';
    $route['login-form'] = "Login";
    $route['admin-dashboard'] = "admin/dashboard";




    $route['news']= 'category/news/1';
    $route['chutkule']= 'category/chutkule/2';
    $route['sports']= 'category/sports/3';
    $route['health']= 'category/bollywood/6';
    $route['education']= 'category/bollywood/7';
    $route['bollywood']= 'category/bollywood/8';
    $route['photogallery']= 'category/photogallery/9';
    $route['fashion']= 'category/fashion/10';



    //$route['news'] = "post/postdetails";
    //$route['(:any)/postdetails'] = "post/postdetails";
    //$route['post/postdetails/(:any)'] = "post/postdetails";

    // $route['p/(:any)'] = 'web/p/$1';

    // $route['ajax-form-validation/post']['post'] = "AjaxFormValidation/validationForm";


    /*
    $route['home.html'] = "home";
    $route['aboutus.html'] = "home/aboutus";
    $route['services.html'] = "home/services";
    $route['portfolio.html'] = "home/portfolio";
    $route['blog.html'] = "home/blog";
    $route['howitwork.html'] = "home/howitwork";
    $route['contactus.html'] = "home/contactus";
    */







    /*
    // it hide the controller name
    $route['default_controller'] = "home";
    $route['about'] = "home/about";
    $route['service'] = "home/service";
    $route['contact'] = "home/contact";

    */


    /*
    // Setting Dynamic URL's Route
    $route['blog/new-topic-list'] = "blog/index/1";
    $route['blog/old-topic-list'] = "blog/index/12";

    */


    /*
    $route['overseas/detail/(:any).html']="overseas/detail/$1/$1";
    $route['make-payment.html']="online_payment/index/$1/$1";
    $route['payment-verification.html']="online_payment/make_payment/$1/$1";
    $route['default_controller'] = "home";
    $route['courses/detail/(:any).html']="courses/detail/$1/$1";
    $route['courses/(:any).html']="courses/sub_course/$1/$1";
    $route['admin'] = "admin/login";
    $route['contact.html'] = "home/contact";
    $route['404_override'] = 'notfound';
    */


    /*
    // Dynamic Product URL
    example.com/mens-clothing/t-shirt/
    example.com/mens-clothing/shirt/
    example.com/mens-clothing/trousers/


    $route['mens-clothing/(:any)'] = "mens/clothing/$1";



    $route['default_controller'] = "product";
    $route['(:any)'] = "product/index/$1";



    */



    /*
    // Adding a URL Suffix

    $config['url_suffix'] = '.html';



    */

    Custom url Routing

    $route['home'] = 'HomeController/index';
    $route['about-us'] = 'HomeController/add_aboutus';
    $route['service'] = 'HomeController/show_services_list';
    $route['photo-gallery'] = 'HomeController/photo_gallery_show';
    $route['news-events'] = 'NewsController/show_news_events';
    $route['contact-us'] = 'ContactusController/add_contactus';




    <a href="<?php echo base_url()?>home"> Home</a>
    <a href="<?php echo base_url()?>about-us"> AboutUs</a>
    <a href="<?php echo base_url()?>service"> Services</a>
    <a href="<?php echo base_url()?>photo-gallery"> Gallery</a>
    <a href="<?php echo base_url()?>news-events"> News & Events</a>
    <a href="<?php echo base_url()?>contact-us"> ContactUs</a>


    Custom url Routing

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

    $route['admin'] = 'admin/login';
    $route['login'] = 'admin/login';
    // $route['login/(:any)']= 'admin/login';

    $route['download']='Download/index';

    $route['blog']='Blog/show_blog';
    $route['(:any)'] = 'tutorial/mytuto/$1/$2';
    $route['(:any)/(:any)'] = 'tutorial/mytutorials/$1/$2';

    //$route['tutorial/(:any)'] = 'tutorial/mytuto/$1/$2';
    //$route['tutorial/(:any)/(:any)'] = 'tutorial/mytuto/$1/$2';

    // $route['blog/(:any)']='welcome/category/$1/$2';

    //$route['(blog/:any)'] = 'women/social';
    // url pass http://www.trendelook.com/blog/xyz
    // It will be directed to http://www.trendelook.com/women/social
    // $route['home.html'] = "home";
    // $route['aboutus.html'] = "home/aboutus";
    $config['url_suffix'] = '.html';





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here