How to route the URI with parameters to a method in codeigniter?

How to route the URI with parameters to a method in codeigniter?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding CodeIgniter Routing: Handling Multiple URI Parameters

You are encountering a common stumbling block when moving from static routes to dynamic parameterized routing in CodeIgniter. The core issue lies in how the framework parses your routes.php configuration against the incoming URL structure. When you define $route['products/display/(:any)'] = 'Products_controller/display';, the :any wildcard captures a single segment. Consequently, when your application receives the request for products/display/2/3/4, the router matches the first part (products/display) and assigns the string 2 to the parameter. The remaining segments, 3 and 4, are left dangling in the URI stream, causing the framework to fail to find a valid controller/method match, resulting in a 404 error.

To pass multiple parameters like 2, 3, and 4 to a single method, you must define the route to explicitly account for every segment you intend to capture. The wildcard syntax requires a specific token for each variable you want to extract. If your URL structure is fixed as /products/display/{id}/{category}/{subcategory}, your configuration should look like this:

$route['products/display/(:num)/(:any)/(:any)'] = 'Products_controller/display/$1/$2/$3';

In this setup, :num restricts the first parameter to numeric values, which is crucial for database integrity checks. The subsequent :any tokens accept strings for the other segments. Crucially, you must append $1, $2, and $3 in the target string. This tells CodeIgniter to pass those specific captured variables as arguments to your controller method. Inside your Products_controller::display() function, these values will be accessible via the $this->uri->segment(1), $this->uri->segment(2), etc., or more conveniently, if you are using a framework version that supports it, they can often be accessed directly as function arguments depending on the specific routing configuration active in your version.

However, there is a significant architectural consideration when aiming to route controller/method requests to completely different controllers, such as mapping a public-facing URI to an internal logic controller. While CodeIgniter's default routing allows you to change the controller name (e.g., $route['products/display'] = 'Admin_products_controller/display';), handling parameters in this cross-routing scenario requires precision. If you simply redirect or load another controller without adjusting the route definition, the parameter parsing logic breaks because the router has already consumed the segments based on the original route pattern.

A more robust approach for complex routing scenarios, especially when you need to normalize incoming URIs regardless of whether they point to a public or internal controller, is utilizing Regex-based routing. This allows you to define patterns that are independent of the folder structure. For instance:

$route['products/display/(:num)/(:any)/(:any)'] = 'controller_method/$1/$2/$3';

This pattern ensures that regardless of how the URL is constructed, the framework extracts the variables and passes them to the designated controller method. You can then handle the logic inside that single controller to decide if it needs to delegate further or process the data directly. This decouples the public API from the internal implementation details.

One common pitfall in this area is assuming that older CodeIgniter versions handle wildcard arguments automatically without explicit definition. Unlike modern frameworks like Laravel, where route definitions often map directly to function signatures, legacy CodeIgniter requires you to manually append the $1, $2 references to your target string unless you are using a custom loader or a specific version of the framework that altered this behavior. Always verify your CodeIgniter version against the routing syntax, as version 2.x and earlier versions behave differently regarding how they parse wildcard arguments compared to version 3.x and later.

If you find yourself needing more flexibility in mapping URIs to internal logic without bloating routes.php, consider implementing a custom route callback or using the $route array's ability to redirect requests. However, for direct parameter passing, sticking to the explicit wildcard syntax ensures the framework correctly parses the URI segments before invoking your method. For developers seeking modern routing patterns and advanced controller organization that might simplify these specific legacy challenges, resources like https://laravelcompany.com offer insights into how contemporary frameworks handle similar concerns with built-in type safety and cleaner syntax.

Our network