Skip to content

HasApiResponse Trait

Anil\FastApiCrud\Concerns\HasApiResponse

Provides JSON response helpers for every HTTP status code. Used by BaseController. Can be used standalone in any controller.

Usage

php
use Anil\FastApiCrud\Concerns\HasApiResponse;

class MyController extends Controller
{
    use HasApiResponse;

    public function example()
    {
        return $this->ok(['message' => 'Hello']);
    }
}

Core Methods

success

php
public function success(array $data = [], int $code = 200): JsonResponse

Returns {"{success_key}": data} where {success_key} is from fast-api.response.success_key config (default: data).

error

php
public function error(string $message = 'Something went wrong', array $data = [], int $status = 400): JsonResponse

Returns {"{error_key}": data, "{message_key}": message} where keys are from config.

1xx Informational

MethodStatusSignature
continue100(array $data = []): JsonResponse
switchingProtocols101(array $data = []): JsonResponse
processing102(array $data = []): JsonResponse
earlyHints103(array $data = []): JsonResponse

2xx Success

MethodStatusSignature
ok200(array $data = []): JsonResponse
created201(array $data = []): JsonResponse
accepted202(array $data = []): JsonResponse
nonAuthoritativeInformation203(array $data = []): JsonResponse
noContent204(): JsonResponse — returns null body
resetContent205(array $data = []): JsonResponse
partialContent206(array $data = []): JsonResponse
multiStatus207(array $data = []): JsonResponse
alreadyReported208(array $data = []): JsonResponse
imUsed226(array $data = []): JsonResponse

3xx Redirection

MethodStatusSignature
multipleChoices300(array $data = []): JsonResponse
movedPermanently301(array $data = []): JsonResponse
found302(array $data = []): JsonResponse
seeOther303(array $data = []): JsonResponse
notModified304(array $data = []): JsonResponse
useProxy305(array $data = []): JsonResponse
temporaryRedirect307(array $data = []): JsonResponse
permanentRedirect308(array $data = []): JsonResponse

4xx Client Error

MethodStatusDefault Message
badRequest400Bad Request
unauthorized401Unauthorized
paymentRequired402Payment Required
forbidden403Forbidden
notFound404Not Found
methodNotAllowed405Method Not Allowed
notAcceptable406Not Acceptable
proxyAuthenticationRequired407Proxy Authentication Required
requestTimeout408Request Timeout
conflict409Conflict
gone410Gone
lengthRequired411Length Required
preconditionFailed412Precondition Failed
contentTooLarge413Content Too Large
uriTooLong414URI Too Long
unsupportedMediaType415Unsupported Media Type
rangeNotSatisfiable416Range Not Satisfiable
expectationFailed417Expectation Failed
imATeapot418I'm a teapot
misdirectedRequest421Misdirected Request
unprocessableContent422Unprocessable Content
locked423Locked
failedDependency424Failed Dependency
tooEarly425Too Early
upgradeRequired426Upgrade Required
preconditionRequired428Precondition Required
tooManyRequests429Too Many Requests
requestHeaderFieldsTooLarge431Request Header Fields Too Large
unavailableForLegalReasons451Unavailable For Legal Reasons

All 4xx methods: (string $message = '...', array $data = []): JsonResponse

5xx Server Error

MethodStatusDefault Message
internalServerError500Internal Server Error
notImplemented501Not Implemented
badGateway502Bad Gateway
serviceUnavailable503Service Unavailable
gatewayTimeout504Gateway Timeout
httpVersionNotSupported505HTTP Version Not Supported
variantAlsoNegotiates506Variant Also Negotiates
insufficientStorage507Insufficient Storage
loopDetected508Loop Detected
notExtended510Not Extended
networkAuthenticationRequired511Network Authentication Required

All 5xx methods: (string $message = '...', array $data = []): JsonResponse

Released under the MIT License.