FuelPHP provides an excellent support for error handling & debugging the application. Let us understand error handling and debugging in this chapter.
Error Handling
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
Internal Errors
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 Error Handling & Debugging 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
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
Debug class provides utility methods to show the detailed information of variables, objects, array, etc. Debug class provides the following methods,
dump
The dump method returns multiple mixed values to the browser in a formatted structured way.
Debug::dump($var1, $var2);
backtrace()
backtrace shows the detailed information about the current execution of code. It shows the PHP file information, current line, and all its previous actions.
Debug::backtrace();
classes()
Returns a list of all classes.
Debug::classes();
interfaces()
Returns a list of all interface classes.
Debug::interfaces();
includes()
Returns a list of all included files currently loaded at the runtime.
Debug::includes();
functions()
Returns a list of all functions.
Debug::functions();
constants()
Returns a list of all constants.
Debug::constants();
extensions()
Returns a list of all extensions.
Debug::extensions();
headers()
Returns a list of all HTTP headers.
Debug::headers();
phpini()
Prints a list of the configuration settings read from php.ini file.
Debug::phpini();
Next Topic : Click Here
Pingback: FuelPHP - Profiler | Adglob Infosystem Pvt Ltd