Search

Laravel Application Helper: abort_if

November 20, 2016 —John Koster

abort_if($boolean, $code, $message = '', array $headers)

The abort_if helper function performs the same basic function as the abort helper function (discussed in the Laravel Application Helper: abort article). The only difference is that the abort_if defines one extra parameter: $boolean.

From the Laravel Application Helper: abort article.

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 argument supplied for $boolean evaluates to true, the abort_if function will abort execution of the application.

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<?php
2 
3// These two function calls are equivalent.
4abort_if(!$user->admin);
5abort_if(($user->admin == false));

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.