FuelPHP - Error Handling & Debugging
FuelPHP provides an excellent support for handling the errors and debugging the application. Let us understand error handling and debugging in this chapter.
FuelPHP error handling is based on exceptions. FuelPHP provides PhpErrorException exception for all old php errors. FuelPHP raises PhpErrorException whenever an error in the PHP code is encountered. FuelPHP also makes it easy to display custom error pages for various HTTP status codes.
File Not Found Error
FuelPHP provides a new exception class, HttpNotFoundException to handle the unknown requests. Sometimes, we may encounter the request which may not be handled. At that time, we can just throw the HttpNotFoundException.
By default, a default page is configured for HttpNotFoundException in the routes configuration file, fuel/app/config/routes.php using 400 entry. Whenever HttpNotFoundException is raised, the request will get redirected to 400 page.
'_404_' => 'welcome/404', // The main 404 route
FuelPHP provides a new exception class, HttpServerErrorException to handle all server errors. Sometimes, we may not be able to process the given request due to internal errors. At that time, we can just throw the HttpServerErrorException.
By default, a default page is configured for HttpServerErrorException in the routes configuration file, fuel/app/config/routes.php using 500 entry. Whenever HttpServerErrorException is raised, the request will get redirected to 500 page.
'_500_' => 'welcome/500', // The main 500 route
This page will log the error, show the will formatted error in the page and occasionally send a notification to the system administrator.
Access Violation Errors
FuelPHP provides a new exception class, HttpNoAccessException to handle the access violations. Sometimes, we may not be able to process the request due to access restriction. At that time, we can just throw the HttpNoAccessException.
By default, a default page is configured for HttpNoAccessException in the routes configuration file, fuel/app/config/routes.php using 403 entry. Whenever HttpNoAccessException is raised, the request will get redirected to 403 page.
'_403_' => 'welcome/403', // The main 403 route
This page will show the access violation information.
Debugging is one of the most frequent activities developing an application. FuelPHP provides a simple class, Debug to handle the debugging activity of the application. Let us learn the Debug class and its methods in this chapter.
Debug class provides utility methods to show the detailed information of variables, objects, array, etc. Debug class provides the following methods,
The dump method returns multiple mixed values to the browser in a formatted structured way.
backtrace shows the detailed information about the current execution of code. It shows the PHP file information, current line, and all its previous actions.
Returns a list of all classes.
Returns a list of all interface classes.
Returns a list of all included files currently loaded at the runtime.
Returns a list of all functions.
Returns a list of all constants.
Returns a list of all extensions.
Returns a list of all HTTP headers.
Prints a list of the configuration settings read from php.ini file.