Here's how you can manipulate or define custom routes in CodeIgniter:
-
Routes Configuration: CodeIgniter allows you to define custom routes in the
application/config/routes.php
file. You can set up specific URL patterns to map to specific controllers/methods.$route['custom/path'] = 'controller/method';
-
This tells CodeIgniter that when a user accesses
http://example.com/custom/path
, it should invoke themethod
method of thecontroller
controller. -
Wildcard Routing: You can use wildcard routing to catch dynamic segments of the URL.
$route['product/(:num)'] = 'catalog/product_lookup_by_id/$1';
-
This route will capture any URL that starts with
product/
followed by a numeric value and pass that value to thecatalog/product_lookup_by_id
method. -
Regular Expression Routing: For more complex routing needs, you can use regular expressions.
$route['product/([a-zA-Z0-9_-]+)'] = 'catalog/product_lookup_by_name/$1';
-
This route will capture any URL that starts with
product/
followed by an alphanumeric value or underscore and hyphen, passing it to thecatalog/product_lookup_by_name
method. -
Default Controller: You can specify a default controller to be invoked when no controller is specified in the URL.
$route['default_controller'] = 'welcome';
-
This tells CodeIgniter to invoke the
index
method of thewelcome
controller if no other controller/method is specified in the URL. -
Group Routing: You can group routes for easier management and organization.
$route['blog'] = 'blog'; $route['blog/(:any)'] = 'blog/$1';
Here, all routes that start with
blog/
will be directed to theblog
controller.