Passing Multiple Variables in URL Using CodeIgniter
You can pass multiple variables via a URL in CodeIgniter, but you have to configure your routing rules first. By default, CodeIgniter expects a single URI segment per method argument unless you explicitly tell it otherwise. When you define getproduct with two parameters like $productID and $factoryID, the framework maps the first URI segment to $productID. The second parameter remains undefined because no additional routing rule exists to capture the next segment.
To fix this, you need to update your router configuration so that multiple segments are passed as an array or individual variables. In CodeIgniter 3, edit application/config/routes.php and add a route that captures both segments:
$route['getproduct/(:num)/(:num)'] = 'main/getproduct/$1/$2';
This tells the router to match URLs like http://localhost/project/main/getproduct/24/45 and pass 24 as $1 and 45 as $2. Your controller method then receives them in order:
public function getproduct($productID, $factoryID) {
// Use both variables here
}
In CodeIgniter 4, the routing syntax is different. Open app/Config/Routes.php and define a route that accepts multiple segments:
use CodeIgniter\Router\RouteCollection;
public function routes()
{
$routes->get('main/getproduct/(:any)/(:any)', 'Main::getproduct/$1/$2');
}
Note the use of (:any) instead of (:num). The (:num) pattern restricts segments to integers only. If your IDs are strings or contain special characters, switch to (:any) to avoid validation errors. You can also specify custom patterns like (:alphanum) for alphanumeric values.
Once the route is defined, ensure your controller method signature matches the number of parameters you want to capture. If you omit a parameter from the method signature, CodeIgniter will not pass that segment unless it's optional or handled differently.
One important limitation: if you have multiple routes that could match the same URL pattern, CodeIgniter uses the first matching route defined in your configuration. This means order matters. Always define more specific routes before generic ones to prevent unexpected behavior.
Another common pitfall is mixing dynamic segments with static paths. If you have a route like getproduct without parameters and another like getproduct/(:num)/(:num), the framework might resolve the URL incorrectly depending on which route is listed first. Review your route order carefully.
If you're using CodeIgniter 4, remember that controller namespaces must be correct. The route 'Main::getproduct/$1/$2' assumes the Main class exists in the expected namespace and has a public getproduct method. Mismatched namespaces or missing classes will cause a 404 error even if routing is correct.
For more complex scenarios, such as passing many variables or mixing static and dynamic segments, consider using route groups or regex patterns in CodeIgniter 4. These allow you to define flexible routes that handle multiple parameters cleanly.
If you need validation for those IDs before processing, use the frameworkâs built-in validation helper. This ensures data integrity right at the entry point of your application.