Currently, CodeIgniter allows developers to override the default 404 error page using the $routes->set404Override() method. This is useful when you want a custom 404 page across the entire application. However, in some cases, especially in larger applications or when using packages, there is a need to define custom 404 error pages for specific routes or route groups. Unfortunately, at the moment, CodeIgniter doesn't offer native support for handling route-specific 404 errors.
But in many web service(API) projects, there is a need to define custom 404 error pages for specific
routes or groups of routes.
Global Impact of $routes->set404Override():
When we use the
$routes->set404Override() method in CodeIgniter, the custom 404 page applies
globally across the entire project. For instance, if you are working on a package that has its own route definitions and use this method like:
PHP Code:
$routes->set404Override('Datamweb\BlankCI\Controllers\Errors::Show404');
This override affects the entire project, not just the specific routes related to your package. As a result, all other parts of the application that depend on their own 404 error behavior will be overridden by this configuration. This makes it impossible to isolate different behaviors for different routes or contexts, leading to conflicts between different components of the project.
Multi-Module or Package Applications:
In projects with multiple modules or packages, different components may require distinct 404 behaviors.
For example, a REST API section might need a minimal JSON response,
while the frontend might require a more user-friendly HTML page for the 404 error. The current implementation makes it difficult to implement different 404 responses for different areas of the application.
Hardcoding 404 Error Handling in Controllers:
Without the ability to define custom 404 pages per route or route group, developers often have to resort to handling errors manually inside controllers. This can be cumbersome and makes the codebase harder to maintain, as each controller has to implement its own logic to check for specific methods and return custom 404 responses.
For example, developers might end up writing code like this:
PHP Code:
$routes->match(['GET', 'POST', 'DELETE','PUT' and ...],'example-route', 'ExampleController::method');
PHP Code:
public function method(): ResponseInterface
{
if (!$this->request->is('post')) {
return $this->formatResponse(
'error',
ResponseInterface::HTTP_METHOD_NOT_ALLOWED,
"The HTTP method({$this->request->getMethod()}) used is not allowed for this endpoint.",
null,
['method' => 'The HTTP method used is not allowed for this endpoint. Allowed methods are: \"POST\"."],
null
);
}
}
While this works, it increases complexity and clutter in controllers, which should ideally focus on core business logic, not error handling.
Proposed Solution:
To address the limitations of the current implementation, I propose adding a method to the `RouteCollection` class that allows developers to set custom 404 error pages per route or route group. Here's how it could work:
New Method withCustom404() for Routes:
Developers should be able to define custom 404 pages for specific routes directly within the route definition. For example:
PHP Code:
$routes->post( 'example-route', 'ExampleController::method')
->withCustom404(function() {
return view('errors/custom404');
});
In this case, if a user accesses the
example-route with an unsupported method or path, the custom 404 page defined in
withCustom404 will be returned instead of the global one.
Support for Grouped Routes:
The method should also support route groups, so developers can apply a custom 404 error page to a set of routes within the same group. For example:
PHP Code:
$routes->group('api', ['namespace' => 'App\Controllers\Api'], function($routes) {
$routes->get('users', 'UserController::index');
$routes->post('users/create', 'UserController::create');
})->withCustom404(function() {
return $this->respond(['error' => 'API not found'], 404);
});;
In this case, all routes under the `/api` namespace will return a
JSON 404 error, while the rest of the application will continue using the default 404 page.
No Global Impact:
The custom 404 page defined using
withCustom404() or similar should only affect the specific route or group of routes. It should not override the global 404 handler for the entire project unless explicitly set at a global level.
Adding the ability to define route-specific 404 error pages would provide a more flexible and modular approach to error handling in CodeIgniter. This feature would be especially beneficial in larger applications or in cases where developers use multiple packages, allowing for distinct behaviors without affecting the global application.