URI Routing
What is URI Routing?
URI Routing maps a URI to a specific controller method, determining how requests are processed.
CodeIgniter supports two types of routing:
Defined Route Routing – Allows manual route definition, offering full control and flexibility over URL structures.
Auto Routing – Automatically maps HTTP requests to the appropriate controller methods based on naming conventions, eliminating the need for manual route setup.
Api.php
and Web.php
Api.php
and Web.php
In a web application, separating routes for APIs and web interfaces is a best practice for managing and maintaining the codebase. Below is the distinction between Api.php
and Web.php
, as well as when to use each file.
Defining Routing Rules in PHPFast
In PHPFast, routes are defined using the $routes
object within dedicated route files. The syntax is straightforward and supports multiple HTTP methods, including GET, POST, PUT, DELETE, and more.
Examples
$routes->get('users/', 'UsersController::list');
$routes->post('users/create', 'UsersController::create');
$routes->get('users/(:num)', 'UsersController::view:$1');
$routes->put('users/edit/(:num)', 'UsersController::edit:$1');
$routes->delete('users/delete/(:num)', 'UsersController::delete:$1');
$routes->get('admin', 'AdminController::index', [\App\Middleware\AuthMiddleware::class]);
Understanding Route Structure
A route consists of two main parts:
Route Path → The URL pattern (relative to the
BaseURL
).Route Handler → The Controller and its corresponding method that should process the request.
Example:
$routes->get('/', 'HomeController::index');
This means that when a user visits /
(the homepage), CodeIgniter will call the index()
method inside the HomeController
.
Defining Routes with HTTP Methods
When specifying a route, you also define the HTTP method it should respond to:
GET Request:
$routes->get('users', 'UsersController::list');
Maps
/users
to thelist()
method inside theUsers
controller.
GET Request with Parameters:
$routes->get('users/1/23', 'UsersController::list/1/23');
Calls
$UsersController->list(1, 23)
, passing1
and23
as parameters.
POST Request:
$routes->post('submit-form', 'UsersController::submitForm')
Handles form submissions via the
submitForm()
method inUsersController
.
Using Placeholders for Dynamic URLs
PHPFast supports placeholders to create flexible routes:
Placeholder
Description
Regex Equivalent
(:any)
Matches any characters except /
.
(.+)
(:segment)
Matches any single segment of the URL, excluding /
.
([^/]+)
(:num)
Matches only numeric values.
(\d+)
(:alpha)
Matches only alphabetic characters (A-Z, a-z).
([a-zA-Z]+)
(:alphanum)
Matches alphanumeric characters (A-Z, a-z, 0-9).
([a-zA-Z0-9]+)
(:hash)
Matches a 32-character hexadecimal hash value.
([a-fA-F0-9]{32})
Example:
1. (:any)
- Matches any string (except /
).
(:any)
- Matches any string (except /
).$routes->get('users/(:any)', 'UsersController::detail/$1');
Example URL: /users/detail
Calls: UsersController::view('detail')
2. (:segment)
– Matches a single URL segment (excluding /).
(:segment)
– Matches a single URL segment (excluding /).$routes->get('users/(:segment)', 'UsersController::show/$1');
Example URL: /users/all
Calls: UsersController::show('all')
3. (:num) - Matches only numeric values.
$routes->get('users/(:num)', 'UsersCotroller::details/$1');
Example URL: /users/123
Calls: UserController::details(123)
4. (:alpha)
– Matches only alphabetic characters (A-Z, a-z).
(:alpha)
– Matches only alphabetic characters (A-Z, a-z).$routes->get('users/(:alpha)', 'UsersController::profile/$1');
Example URL: /user/php_fast
Calls: UsersController::profile('php_fast')
5. (:alphanum) – Matches alphanumeric characters (A-Z, a-z, 0-9).
$routes->get('users/(:alphanum)', 'UsersController::execute/$1');
Example URL: /users/abc123
Calls: UsersController::execute('abc123')
6. (:hash) – Matches a 32-character hexadecimal hash value.
$routes->get('verify/(:hash)', 'AuthController::verify/$1');
Example URL: /verify/a94a8fe5ccb19ba61c4c0873d391e987
Calls: AuthController::verify('a94a8fe5ccb19ba61c4c0873d391e987')
Global Options
The given lines define different types of routes in PHPFast using $routes
. Each method corresponds to a specific HTTP request type, which determines how the route processes incoming requests.
// Route that only accepts GET requests
$routes->get('product/(:num)/(:string)', 'ProductController::show:$1:$2');
// Route that only accepts POST requests (for form submissions, data creation)
$routes->post('product/create', 'ProductController::create');
// Route that only accepts PUT requests (used for updating resources)
$routes->put('product/update/(:num)', 'ProductController::update/$1');
// Route that only accepts DELETE requests (used to delete resources)
$routes->delete('product/delete/(:num)', 'ProductController::delete/$1');
// Applying middleware to a single route
$routes->get('admin', 'AdminController::index', [\App\Middleware\AuthMiddleware::class]);
Last updated
Was this helpful?