Laravel is one of the best web development frameworks.
But does it have any error handling feature?
Yes. There is a default feature that helps to debug by making Laravel show PHP errors in the browser.
At Bobcares, we often get requests to turn on Laravel PHP errors as part of our Server Management Services.
Today, let’s see how our Support Engineers use the debug feature to troubleshoot errors.
How to make Laravel show PHP errors
By default, the Laravel framework has an error handling feature. The debug option in the configuration file determines the error display at the user end.
Usually, the Laravel configuration file that enables debugging is the config/app.php. The debug option is set to default according to the APP_DEBUG variable in the .env file.
Basically, the .env file is a way to load custom configuration variables for Laravel. Hence, to make custom changes to Laravel, there is no need to modify web server files like .htaccess, virtual hosts and so on.
So, in the .env file, the APP_DEBUG variable is set to true to show PHP errors. This, in turn, changes the debug value in app.php to true. The debug option in app.php appear as,
Due to security reasons, showing errors all the time is also not recommended. Displaying Laravel PHP errors all the time in the browser will make the website vulnerable to website attacks.
Hence, our Support Team enables this feature only when we need to troubleshoot any error. After fixing the error, we turn off the debug feature.
Causes and fix for Laravel Debug not working
Sometimes, even after turning the debug value to true, Laravel does not show PHP errors. This is also a common request we often get from our customers. Let’s see how our Support Team fix this error.
1. Configuration setting in .env file
Some customers just change the debug value in config/app.php alone. This works fine in the production environment, but in the local environment, this does not display PHP errors.
So, our Support Team make sure that the following changes are made in .env file
APP_ENV=localAPP_DEBUG=true
When the configuration settings are proper then Laravel shows PHP errors in the browser. Due to security reasons, we always recommend our customers to turn on the debug feature only for troubleshooting.
2. Improper folder permissions
In some situations, the incorrect permissions of the storage and vendor directory in Laravel also causes the error. In this case, even the debug update in the .env file does not show PHP errors.
Usually, webserver needs write-access over Laravel folders storage and vendor.
For example, if the Apache web server is using a suPHP handler, then the PHP script runs with user permission. Thus the webserver will also have write permissions on the Laravel folders too.
But if the Apache webserver is running as a DSO module, then PHP application runs under nobody ownership. In this case, we need to give write permissions to the nobody user for using the Laravel folders.
Thus, our Support Engineers check the folder permissions and change it according to the web server in use.
3. The default setting in app.php
Occasionally, PHP files in the Laravel directory structure have some default settings that disable the debug feature.
For instance, the bootstrap directory contains an app.php file that loads the Laravel framework.
The app.php has some default settings, that comment some useful code related to debugging.
To fix this, we uncomment the line Dotenv::load(__DIR__.'/../');
in bootstrap/app.php to show PHP errors in Laravel.
Conclusion
So far, we saw how to make Laravel show PHP errors. We also discussed the possible errors that prevent error display in Laravel and how our Support Engineers fix them.
Excelente.