Skip to content

3. HTTP Status Codes

necipallef edited this page Jul 19, 2018 · 4 revisions

It is possible to return a desired HTTP status code by throwing an exception, like below:

public function configureEndpointBag($endpoint_bag)
{
    $endpoint_bag->create('/')
        ->addGET('get')
        ->addPOST('create');
}

public function get() {
    return 'inside get function';
}

public function post() {
    throw new \Fabstract\Component\Http\Exception\StatusCodeException\ForbiddenException();
}

Now doing a HTTP POST

curl -i -X POST yourdomain.postfix/user -H"Content-Type: application/json"

will result in following:

HTTP/1.1 403 Forbidden
Content-Type: application/json

{"error_message":"forbidden","status":"failure"}

You can use many of the status code exceptions that come with Fabstract

ForbiddenException will be HTTP 403

ConflictException will be HTTP 409

UnauthorizedException will be HTTP 401

UnprocessableEntityException will be HTTP 422

etc.

Internal Server Errors

When an unexpected error happens, system will give HTTP 500. You don't have to do anything.

Clone this wiki locally