Throwing HttpExceptions


You want to abort your application.

You want to return a non-200 status code back to the user indicating the request was not successful.


Use the App::abort() method.

When something's not found use a 404 code.


This halts your current processing by throwing a NotFoundException, specifically a Symfony\Component\HttpKernel\Exception\NotFoundHttpException exception.

You can also provide a message, specifying what wasn't found.

App::abort(404, 'User not found');

For other HTTP Exceptions you'll normally throw a code in the 400 range for client errors and in the 500 range for server errors. See the List of HTTP Status Codes.

Here's a few examples.

// Unauthorized
App::abort(401, 'Not authenticated');

// Forbidden
App::abort(403, 'Access denied');

// Internal Server Error
App::abort(500, 'Something bad happened');

// Not implemented
App::abort(501, 'Feature not implemented');

For non-404 errors you can also specify additional headers the response should return.

App::abort(401, 'Not authenticated', ['WWW-Authenticate' => 'Basic']);

Non-404 status codes will throw a HttpException. Specifically, a Symfony\Component\HttpKernel\Exception\HttpException.


Your application can trap these exceptions.

See Registering a 404 Error Handler.

comments powered by Disqus