Skip to content

HttpStatusCode Class

Provides HTTP status code constants and helper methods.

Usage

php
use Fluxor\HttpStatusCode;

// Use constants
Response::error('Not Found', HttpStatusCode::NOT_FOUND);
Response::redirect('/login', HttpStatusCode::FOUND);

// Get status code message
$message = HttpStatusCode::message(404);  // "Not Found"

// Check status code category
if (HttpStatusCode::isSuccess(200)) { ... }
if (HttpStatusCode::isError(404)) { ... }

Available Constants

1xx: Informational

ConstantValueDescription
CONTINUE100Continue
SWITCHING_PROTOCOLS101Switching Protocols
PROCESSING102Processing
EARLY_HINTS103Early Hints

2xx: Success

ConstantValueDescription
OK200OK
CREATED201Created
ACCEPTED202Accepted
NON_AUTHORITATIVE_INFORMATION203Non-Authoritative Information
NO_CONTENT204No Content
RESET_CONTENT205Reset Content
PARTIAL_CONTENT206Partial Content
MULTI_STATUS207Multi-Status
ALREADY_REPORTED208Already Reported
IM_USED226IM Used

3xx: Redirection

ConstantValueDescription
MULTIPLE_CHOICES300Multiple Choices
MOVED_PERMANENTLY301Moved Permanently
FOUND302Found
SEE_OTHER303See Other
NOT_MODIFIED304Not Modified
USE_PROXY305Use Proxy
SWITCH_PROXY306Switch Proxy
TEMPORARY_REDIRECT307Temporary Redirect
PERMANENT_REDIRECT308Permanent Redirect

4xx: Client Errors

ConstantValueDescription
BAD_REQUEST400Bad Request
UNAUTHORIZED401Unauthorized
PAYMENT_REQUIRED402Payment Required
FORBIDDEN403Forbidden
NOT_FOUND404Not Found
METHOD_NOT_ALLOWED405Method Not Allowed
NOT_ACCEPTABLE406Not Acceptable
PROXY_AUTHENTICATION_REQUIRED407Proxy Authentication Required
REQUEST_TIMEOUT408Request Timeout
CONFLICT409Conflict
GONE410Gone
LENGTH_REQUIRED411Length Required
PRECONDITION_FAILED412Precondition Failed
PAYLOAD_TOO_LARGE413Payload Too Large
URI_TOO_LONG414URI Too Long
UNSUPPORTED_MEDIA_TYPE415Unsupported Media Type
RANGE_NOT_SATISFIABLE416Range Not Satisfiable
EXPECTATION_FAILED417Expectation Failed
IM_A_TEAPOT418I'm a teapot
MISDIRECTED_REQUEST421Misdirected Request
UNPROCESSABLE_ENTITY422Unprocessable Entity
LOCKED423Locked
FAILED_DEPENDENCY424Failed Dependency
TOO_EARLY425Too Early
UPGRADE_REQUIRED426Upgrade Required
PRECONDITION_REQUIRED428Precondition Required
TOO_MANY_REQUESTS429Too Many Requests
REQUEST_HEADER_FIELDS_TOO_LARGE431Request Header Fields Too Large
UNAVAILABLE_FOR_LEGAL_REASONS451Unavailable For Legal Reasons

5xx: Server Errors

ConstantValueDescription
INTERNAL_SERVER_ERROR500Internal Server Error
NOT_IMPLEMENTED501Not Implemented
BAD_GATEWAY502Bad Gateway
SERVICE_UNAVAILABLE503Service Unavailable
GATEWAY_TIMEOUT504Gateway Timeout
HTTP_VERSION_NOT_SUPPORTED505HTTP Version Not Supported
VARIANT_ALSO_NEGOTIATES506Variant Also Negotiates
INSUFFICIENT_STORAGE507Insufficient Storage
LOOP_DETECTED508Loop Detected
NOT_EXTENDED510Not Extended
NETWORK_AUTHENTICATION_REQUIRED511Network Authentication Required

Helper Methods

message(int $code): string

Returns the standard message for a status code:

php
echo HttpStatusCode::message(404);  // "Not Found"
echo HttpStatusCode::message(200);  // "OK"
echo HttpStatusCode::message(418);  // "I'm a teapot"

Category Check Methods

php
HttpStatusCode::isInformational(100);  // true (1xx)
HttpStatusCode::isSuccess(200);        // true (2xx)
HttpStatusCode::isRedirection(301);    // true (3xx)
HttpStatusCode::isClientError(404);    // true (4xx)
HttpStatusCode::isServerError(500);    // true (5xx)
HttpStatusCode::isError(404);          // true (4xx or 5xx)
HttpStatusCode::isError(200);          // false

Example Usage

php
use Fluxor\HttpStatusCode;
use Fluxor\Response;

// API endpoint
Flow::GET('/api/users')->do(function($req) {
    $users = $this->findAllUsers();
    
    if (empty($users)) {
        return Response::error('No users found', HttpStatusCode::NOT_FOUND);
    }
    
    return Response::success($users);
});

// Redirect with appropriate status
Flow::POST('/login')->do(function($req) {
    if ($this->authenticate($req->input('email'), $req->input('password'))) {
        return Response::redirect('/dashboard', HttpStatusCode::FOUND);
    }
    return Response::error('Invalid credentials', HttpStatusCode::UNAUTHORIZED);
});

Notes

  • Constants follow HTTP specification standards
  • Use constants instead of magic numbers for better code readability
  • Category methods are useful for generic error handling

Released under the MIT License.