Customizable 404 Error Pages for Specific Routes |
In my project, I solved this issue by overriding the RouteCollection class and adding a method to handle custom 404 pages for specific routes. The method I use is called setCustom404ForRoute, which works as follows:
PHP Code: /** To manage the responses, I've created a controller that sends error messages in a detailed JSON format. For example, if a GET method is used instead of POST on a specific route, the response looks like this: PHP Code: <?php And my Routes: PHP Code: $routes->post('api/v1/ticket-history/record-change', 'TicketHistoryController::recordChange')->setCustom404ForRoute('Datamweb\BlankCI\Controllers\Errors::Show404', 'api/v1/ticket-history/record-change', 'post'); Here’s an example of the response for a GET('GET', 'POST', 'DELETE','PUT' and ... ) (allowed method (POST))request when the HTTP method is not allowed: Code: { In case the correct POST method is used but the data fails validation, the response includes a 400 error with detailed validation issues: Code: { Additionally, other routes can still use CodeIgniter's default 404 page or be overridden with a custom page using the method $routes->set404Override(). This approach allows developers to easily manage their custom 404 pages while maintaining flexibility in handling specific routes. @kenjis This idea stems from a real need in a practical project, which is why I believe, although it requires further review for better implementation, adding it to CodeIgniter would be an excellent feature and would provide greater flexibility. and what you mentioned does not yield the same result. |
Messages In This Thread |
Customizable 404 Error Pages for Specific Routes - by datamweb - 08-22-2024, 04:22 AM
RE: Customizable 404 Error Pages for Specific Routes - by ozornick - 08-22-2024, 11:06 AM
RE: Customizable 404 Error Pages for Specific Routes - by kenjis - 08-22-2024, 06:38 PM
RE: Customizable 404 Error Pages for Specific Routes - by kenjis - 08-22-2024, 06:55 PM
RE: Customizable 404 Error Pages for Specific Routes - by datamweb - 08-22-2024, 09:13 PM
RE: Customizable 404 Error Pages for Specific Routes - by kenjis - 08-23-2024, 12:05 AM
RE: Customizable 404 Error Pages for Specific Routes - by datamweb - 08-23-2024, 12:42 AM
RE: Customizable 404 Error Pages for Specific Routes - by kenjis - 08-23-2024, 02:09 AM
RE: Customizable 404 Error Pages for Specific Routes - by ozornick - 08-23-2024, 02:48 AM
|