Laravel 5: Throwing HTTP Exceptions With abort

April 14, 2018 —John Koster

The abort function will throw an instance of
"Symfony\Component\HttpKernel\Exception\HttpException" with the given $code, $message and any $headers supplied. These exceptions can be caught and handled through the application's kernel.

If the $code supplied is 404, the abort function will return an instance of
"Symfony\Component\HttpKernel\Exception\NotFoundHttpException" using only the user supplied $message.

#Signature

The signature of the abort function is:

1function abort(
2 $code,
3 $message = '',
4 array $headers = []`
5);

#Example Use

The following example assumes that some $user object exists with the property admin. The example will check to make sure that the admin property is true. If not, the code will abort with a 401 (unauthorized access) error code.

1 
2if (!$user->admin) {
3 abort(401);
4}

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.