Author: Decode Web
Updated on: 2019-09-19
Posted on: 2019-09-19
Categories: PHP Tutorials, PHP Security, PHP community
Good developers should be ready to handle them in the best possible way to minimize the harm that the errors may cause to the application users and providers
Read this article to learn about the most common mistakes committed by less experienced developers and how can you fix them.
Laravel is beautiful, no doubt, but I prefer to believe that its error reporting support in the development environment is too much haunted.
As far as I know everyone has seen a black and white screen with millions of characters arranged line by line, even if there is a semicolon (;) missing.
The main topic of this article is the top 5 common errors or exceptions caused mistakes that less experienced developers make while working on Laravel. Those errors are:
419 Error / Page Expired
File Permission for storage
ReflectionException / Class does not exist]
Let's take a look at these errors in more detail.
I prefer to believe that this is one the most frequent errors that occurs in the form of PHP exception, as it says "Method is not allowed".
But where is the code that is causing this error? To find out you can simply go to web.php or api.php scripts where you have configured the request routes and check if any route or URL pattern is using some other type of HTTP method instead of the intended method.
For example, your route may be configured to use the POST method but the current request is done using the GET method. Since Laravel is very focused on security, it does not allow the POST method route to be called as GET route hence this exception occurs.
2. 419 Error / Page Expired
Again, this exception is caused by the on application security protection support built-in Laravel. Error 419 or Page Expired may occur when we do not include CSRF_TOKEN form input in the page HTML body.
Forms are usually the way to pass data to another page to be handled by another controller.
Excluding Routes from CSRF Protection
Sometimes there may be situations where on which we may want to have routes to exist without requiring the CSRF_TOKEN. We can simply add those URIs or routes in VerifyCsrfToken middleware's except array like this
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
* The URIs that should be excluded from CSRF verification.
* @var array
protected $except = [
3. File Permissions to Access the Storage
In the development environment, Laravel generates lots of error log messages in storage directory as files. These log files are named according to the log messages' date, like for instance 2019-08-15.log.
So to write to those log files in Linux distributions like Ubuntu, we need to give the root user (Super User) access permissions to storage directory, for instance like this:
sudo chmod -R 776 /storage
Similarly, for the bootstrap directory we often get errors like this. Hence the best practice is to give both of these directories access to write.
sudo chmod -R 776 /bootstrap /storage
4. ReflectionException / Class does not exist
Like this heading suggests, this exception occurs when we reference to any class but that class file is not present at expected directory location.
For example, in the web.php script, we define a route or URI and map it to a specific Controller and the respective handling function.
If that particular controller class is not present in the expected directory, I mean, the class file path may be wrong or controller function was not yet created.
The same applies to Models, Events, ServiceProviders, etc..
5. 500 Error
The 500 error is one of the scariest errors even for an experienced developer.
To debug this error first I recommend checking first if the Web server is working in good conditions. Then I recommend to check if site source code is working correctly.
Many a times, less experienced developers make mistakes like for instance redirect to the same page to a specific controller function, making the server overload and not be able to handle any more requests.
If anything goes wrong on server, the HTTP status code will always be 500 even if the root cause varies.
Try to write good code using for instance try-catch blocks. This way you can avoid getting HTTP status 500 without good clues of what caused it.
In any case, you should try to enable PHP error log making it output PHP runtime errors to a error log file. You can achieve this by changing your php.ini configuration file in your PHP development environment using values like these:
error_reporting = E_ALL display_errors = Off display_startup_errors = Off log_errors = On log_errors_max_len = 0 ignore_repeated_errors = On ignore_repeated_source = Off report_memleaks = On track_errors = On html_errors = Off error_log = "/tmp/php_error_log";
Well I hope you liked this article about common errors when you use are developing PHP applications with the Laravel framework or even other frameworks or methods.
This article was originally posted on my blog and was edited by a PHP Classes site moderator. If you liked this article please share it with your colleague PHP developers. If you have questions or comments, please post a message below this article.
You need to be a registered user or login to post a comment
1,550,765 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on: