How to create routes in the codeigniter
// File: application/config/routes.php
// Default route
$route['default_controller'] = 'welcome';
// Custom route for specific URL
$route['products'] = 'catalog/products';
// Route with parameters
$route['product/(:num)'] = 'catalog/product_lookup/$1';
// Regular expression route
$route['blog/(:any)'] = 'blog/posts/$1';
Explanation:
- Default Route:
$route['default_controller'] = 'welcome';
Sets the default controller for the application. When no controller is specified in the URL, it directs to the 'welcome' controller by default.
Custom Route for Specific URL:
$route['products'] = 'catalog/products';
Directs requests to the 'products' URL to the 'catalog' controller's 'products' method.
Route with Parameters:
$route['product/(:num)'] = 'catalog/product_lookup/$1';
Matches URLs like 'product/{number}' and redirects them to the 'catalog' controller's 'product_lookup' method while passing the number as a parameter.
Regular Expression Route:
$route['blog/(:any)'] = 'blog/posts/$1';
- Matches URLs like 'blog/{any}' and routes them to the 'blog' controller's 'posts' method while passing the parameter 'any'. This '(:any)' segment in the route allows any characters in the URL to match this route. ```