CodeIgniter Routing: Understanding the System and Best Practices
Course Title: Mastering CodeIgniter Framework: Fast, Lightweight Web Development Section Title: Routing, Controllers, and Views in CodeIgniter Topic: Understanding CodeIgniter’s routing system.
Introduction
Routing is an essential part of any web framework, as it determines how incoming requests are handled and mapped to the correct controllers and methods. In CodeIgniter, the routing system is flexible, configurable, and easy to understand. In this topic, we'll delve into the world of CodeIgniter routing, exploring its features, advantages, and best practices.
The Basics of Routing in CodeIgniter
In CodeIgniter, routing is handled by the system/core/Router.php
file. The routing system uses a combination of URL segments to determine which controller and method to call. A typical CodeIgniter URL looks like this:
https://example.com/controller/class/method/
Here's a breakdown of the URL components:
- Controller: The name of the controller class.
- Class: This is optional and refers to a specific method within the controller.
- Method: The name of the method within the controller that will handle the request.
How CodeIgniter's Routing System Works
When a request is received by the CodeIgniter application, the routing system follows these steps to determine which controller and method to call:
- URI Detection: The routing system checks for the presence of a URI in the request. If no URI is present, the
default_controller
specified in theapplication/config/routes.php
file is used. - Route Configuration: The routing system checks for any custom route configurations in the
application/config/routes.php
file. - Controller and Method Determination: Based on the URI or custom route configuration, the routing system determines which controller and method to call.
Types of Routing in CodeIgniter
CodeIgniter supports multiple types of routing:
- Legacy Routing: This routing type uses the URL segments to determine the controller and method.
- URI Routing: This routing type uses the
uri
segment to determine the controller and method. - Custom Routing: This routing type allows you to define custom routes using the
$routes
array in theapplication/config/routes.php
file.
Custom Routing in CodeIgniter
Custom routing allows you to define custom routes using the $routes
array in the application/config/routes.php
file. Here's an example:
$routes['products'] = 'product_controller/index';
$routes['products/(:num)'] = 'product_controller/view_product/$1';
In the above example, the first route maps the /products
URI to the index
method of the product_controller
controller. The second route maps the /products/{id}
URI to the view_product
method of the product_controller
controller, passing the {id}
segment as a parameter.
Regular Expression Routing in CodeIgniter
CodeIgniter also supports regular expression routing, which allows you to define routes using regular expressions. Here's an example:
$routes['product/([a-zA-Z0-9]+)/item/(\w+)(?:/page/)?(\d*)'] = 'product_controller/$1/$2/$3';
In the above example, the route uses regular expressions to match the /product/{product_code}/item/{item_name}/page/{page_number}
URI and maps it to the product_controller
controller.
Named Routing in CodeIgniter
CodeIgniter 4.0 introduced named routing, which allows you to define routes with a name. Here's an example:
$routes->get('/products', 'ProductController::index')->setName('products.index');
Named routing allows you to generate URLs using the route name instead of the actual URL.
Best Practices for CodeIgniter Routing
Here are some best practices for CodeIgniter routing:
- Keep your routes organized and consistent.
- Use custom routing to simplify complex URLs.
- Use regular expression routing to match complex URL patterns.
- Use named routing to simplify URL generation.
Conclusion
In conclusion, CodeIgniter's routing system is flexible, configurable, and easy to use. By understanding the basics of routing in CodeIgniter, you can create robust and scalable web applications. In the next topic, we'll cover creating and organizing controllers for application logic.
What to do next?
Do you have any questions or need help with CodeIgniter routing? Leave a comment below or refer to the official CodeIgniter documentation (https://codeigniter.com/user_guide/index.html) for more information.
Also, you can practice what you have learned by trying to implement routing in your own project.
Next Topic: Creating and Organizing Controllers for Application Logic.
Images

Comments