New from Chevere is the Http package. This software provides tooling for building HTTP components.
# Http Controller
The Http Controller is built on top of the Controller component and it enables to define the I/O of an HTTP endpoint, with support for query string, body, file uploads and path variables, which can be hinted using StringAttr attribute.
use Chevere\Attribute\StringRegex;
use Chevere\Http\Controller;
class MyController extends Controller
{
public function run(
#[StringRegex('/^[0-9]{2}$/')]
string $id
): array
{
return [];
}
}
# Attributes
The Http package provides general use-case attributes, designed to be used at both Controller and Middleware.
With attributes is easier to separate concerns and to drive tailored system needs.
# Header attribute
With the Header attribute you can define multiple response header lines:
use Chevere\Http\Attributes\Header;
#[
Header('Content-Disposition', 'attachment'),
Header('Content-Type', 'application/json')
]
Use function classHeaders
to get a list of Header attributes for a given class.
# Status attribute
With the Status attribute you can indicate response status codes:
use Chevere\Http\Attributes\Status;
#[Status(200, 404)]
Use function classStatus
to get a the Status attribute for a given class.
# Exceptions
The Http package provides exceptions for both Http Controller and Middleware with ControllerException
and MiddlewareException
respectively.
I added these because I don't like using middleware to stop a request by returning a new response as I prefer throwing a known exception at the affected concern. This enables to drop an unwanted extra responsibility.
# Learn more
Head over to the documentation and check the source at GitHub.