How to route a URL in CodeIgniter to a different controller class or method?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to route a URL in CodeIgniter to a different controller class or method?

    By default, CodeIgniter routes URLs to the class (first URL segment) and method (second URL segment) specified in the URL string. For example, "example.com/home/about" would be routed to the "about" method within the "home" controller class. If a method is not specified in the URL string, the default method is executed within the specified controller class. However, in some situations you may need to dynamically route certain URL strings to different classes or methods within your application, and CodeIgniter allows you to do this in a very simple way.

    So in my example, I want to route "example.com/r/eukhost" to a method within a different controller, because the "r" controller does not exist in my application. To change the route, I need to go to the application/config folder and open "routes.php". At the bottom of this file notice "RESERVED ROUTES" and in my case I need to add the following line:

    PHP Code:
    $route['r/(:any)'] = '/home/r/'
    This would route anything starting with "r" in the URL string (such as "example.com/r/eukhost", "example.com/r/example" etc.) to the r() method within the home controller.
Working...
X