A Laravel package for managing HTTP status codes, methods and other related HTTP enums.
LaraHttpEnums is a simple, easy-to-use package that provides a collection of HTTP status codes, methods and related enums to be used in your Laravel projects. This package helps streamline working with HTTP-related constants, improving readability and consistency in your code.
You can install the package via Composer:
composer require egbosionu/lara-http-enums
use Egbosionu\LaraHttpEnums\StatusCode;
// Basic usage
$status = StatusCode::OK; // 200
$status = StatusCode::NOT_FOUND; // 404
// Get reason phrase
$phrase = StatusCode::OK->getReasonPhrase(); // "OK"
// Check status type
$status = StatusCode::NOT_FOUND;
$status->isClientError(); // true
$status->isError(); // true
$status->isSuccessful(); // false
// Convert from integer
$status = StatusCode::fromInt(404); // StatusCode::NOT_FOUND
$status = StatusCode::tryFromInt(404); // StatusCode::NOT_FOUND or null if invalid
// Convert from name
$status = StatusCode::fromName('NOT_FOUND'); // StatusCode::NOT_FOUND
$status = StatusCode::tryFromName('NOT_FOUND'); // StatusCode::NOT_FOUND or null if invalid
use Egbosionu\LaraHttpEnums\Method;
// Basic usage
$method = Method::GET;
$method = Method::POST;
// Check method properties
$method = Method::GET;
$method->isSafe(); // true - doesn't modify resources
$method->isIdempotent(); // true - multiple identical requests have same effect as single request
// Convert from string
$method = Method::fromName('GET'); // Method::GET
$method = Method::tryFromName('GET'); // Method::GET or null if invalid
use Egbosionu\LaraHttpEnums\ReasonPhrase;
use Egbosionu\LaraHttpEnums\StatusCode;
// Get reason phrase from status code
$phrase = ReasonPhrase::fromStatusCode(StatusCode::NOT_FOUND); // ReasonPhrase::NOT_FOUND
$text = ReasonPhrase::fromStatusCode(StatusCode::NOT_FOUND)->value; // "Not Found"
// Try to get reason phrase
$phrase = ReasonPhrase::tryFromStatusCode(StatusCode::NOT_FOUND); // ReasonPhrase::NOT_FOUND or null if invalid
-
Type-safe HTTP status codes with integer values
-
Type-safe HTTP methods with string values
-
Standard reason phrases for all status codes
-
Helper methods for checking status code categories
-
Helper methods for checking method properties
-
Case-insensitive method name parsing
-
Null-safe conversion methods
-
Full PSR-4 autoloading support
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.