You are on page 1of 4

5 common errors in laravel which haunt new

developers
Source:
https://decodeweb.in/php/php-frameworks/laravel-framework/5-common-errors-in-laravel-which-haunt-new-develop
ers/

Hello readers ! hope you are doing fine and enjoying my blog. Laravel is beautiful, no doubt, but its error reporting
while in 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.

Our today’s topic would be the top 5 common errors / exceptions or simply the mistakes new developers make
while working on Laravel which are:

1. MethodNotAllowedHttpException
2. 419 Error / Page Expired
3. File Permission for storage
4. ReflectionException / Class does not exist]
5. 500 Error
1. MethodNotAllowedHttpException

This is the most frequent occurring exception, as it says “Method is not allowed” but where ? Simply go to ​web.php
or api.php​ where you have written the routes and check if any route / url is using some other type of http method
instead of the intended method.

For example, if your route is using POST as method but you are calling as GET. As Laravel is too much concerned
about security, it will not allow POST route to be called as GET route hence this exception occurs.

2. 419 Error / Page Expired

Again, this exception is based on in-built application security. Error 419 or Page Expired comes when we do not
include CSRF_TOKEN in the html form body, since forms are usually the way to POST data to another file or
controller.

Excluding routes from CSRF Protection


Sometimes there may be situations where we might want to allow some routes without CSRF_TOKEN, we can
simply add those URIs or routes in VerifyCsrfToken middleware’s except array like this
<?php

namespace​ ​App​\​Http​\​Middleware​;

use​ ​Illuminate​\​Foundation​\​Http​\​Middleware​\​VerifyCsrfToken​ ​as​ ​Middleware​;

​ erifyCsrfToken​ ​extends​ ​Middleware


class​ V
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected​ $except = [
'stripe/*'​,
'http://example.com/foo/bar'​,
'http://example.com/foo/*'​,
];
}

3. File Permission for storage


In development environment, laravel generates lots of error logs in storage directory as files named according to
dates, for example 2019-08-15.log so to write those In linux distros like Ubuntu, we need to give Super User
permission to storage directory.

sudo chmod -R 776 ​/storage

Same way for bootstrap directory we often get error like this. Hence best practice in would be 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 give reference to any class but that class is not present
at that location.
For example, in web.php, we define a route or URI and map it to a particular Controller and its function, if that
particular controller class is not present there, that is, path may be wrong or controller itself is not created. Same
applies to Models, Events, ServiceProviders etc.

5. 500 Error
500 error is one of the scariest even for experienced developer, no offense, to debug this error first we need to
check if server is all good, second check will be how is a logic applied to source code. Many a times, as new
developers, we tend to make silly mistakes like, we redirect to the same page, to be specific a controller function,
hence on server end its stack gets full thus it is not available to handle more request.

If anything goes wrong on server, error code will be the same 500 even if root cause be whatever, try to write
sensitive codes in try-catch blocks at least we can be prevented by getting 500 errors.

You might also like