chevere / http
A chevere http package
Installs: 5 481
Dependents: 6
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 3
Requires
- php: ^8.1
- chevere/action: ^1.1.1
- chevere/data-structure: ^1.1.1
- chevere/message: ^1.0.0
- chevere/parameter: ^1.1.2
- nyholm/psr7: ^1.8
- psr/http-factory: ^1
- psr/http-message: ^1|^2
- psr/http-server-handler: ^1
- psr/http-server-middleware: ^1
Requires (Dev)
- chevere/var-dump: ^2.0
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.5
- symplify/easy-coding-standard: ^12.1
README
Summary
The Http package provides tooling for building HTTP components.
::: tip 💡 Http introduction Read Chevere Http at Rodolfo's blog for a compressive introduction to this package. :::
Installing
Http is available through Packagist and the repository source is at chevere/http.
composer require chevere/http
Controller
The Controller in Http is a special Controller meant to be used in the context of HTTP requests. It extends Action by adding request parameters (query string, body, files) and attributes for statuses and headers.
use Chevere\Http\Controller; class UsersPostController extends Controller { // ... }
Accept Query
Define accepted parameters for query string using the acceptQuery
method.
use Chevere\Parameter\Interfaces\ArrayStringParameterInterface; use function Chevere\Parameter\arrayp; use function Chevere\Parameter\parameters; use function Chevere\Parameter\string; public static function acceptQuery(): ArrayStringParameterInterface { return arrayp( foo: string('/^[a-z]+$/'), ); }
Accept Body
Define accepted parameters for body using the acceptBody
method.
use Chevere\Parameter\Interfaces\ArrayParameterInterface; use function Chevere\Parameter\arrayp; use function Chevere\Parameter\parameters; use function Chevere\Parameter\string; public static function acceptBody(): ArrayParameterInterface { return arrayp( bar: string('/^[1-9]+$/'), ); }
Accept Files
Define accepted parameters for FILES using the acceptFiles
method.
use Chevere\Parameter\Interfaces\ArrayParameterInterface; use function Chevere\Parameter\arrayp; use function Chevere\Parameter\file; public static function acceptFiles(): ArrayParameterInterface { return arrayp( myFile: file(), ); }
With Query
Set query parameters using the withQuery
method. It will only accept arguments complying with Accept Query.
$controller = $controller ->withQuery($_GET);
With Body
Set body parameters using the withBody
method. It will only accept arguments complying with Accept Body.
$controller = $controller ->withBody($_POST);
With Files
Set files parameters using the withFiles
method. It will only accept arguments complying with Accept Files.
$controller = $controller ->withFiles($_FILES);
Query
Use method query
to read query parameters.
$query = $controller->query();
Body
Use method body
to read the body parameters.
$post = $controller->body();
Files
Use method files
to read the files parameters.
$files = $controller->files();
Middleware
Define PSR Middleware collections using middlewares
function.
use function Chevere\Http\middlewares; $middlewares = middlewares( MiddlewareOne::class, MiddlewareTwo::class );
Middleware priority goes from top to bottom, first in first out (FIFO).
Attributes
Use attributes to add context for Controller and Middleware.
Request
Request metadata can be defined using the Request
attribute. It supports to define multiple Header arguments.
use Chevere\Http\Attributes\Request; use Chevere\Http\Header; use Chevere\Http\Controller; #[Request( new Header('Accept', 'application/json'), new Header('Connection', 'keep-alive') )] class ResourceGetController extends Controller
Use function getRequest
to read the Request
attribute.
use function Chevere\Http\getRequest; getRequest(ResourceGetController::class);
Response
Response metadata can be defined using the Response
attribute. It supports to define Status and multiple Header arguments.
use Chevere\Http\Attributes\Response; use Chevere\Http\Header; use Chevere\Http\Controller; #[Response( new Status(200), new Header('Content-Disposition', 'attachment'), new Header('Content-Type', 'application/json') )] class ResourceGetController extends Controller
Use function getResponse
to read the Response
attribute.
use function Chevere\Http\getResponse; getResponse(ResourceGetController::class);
Documentation
Documentation is available at chevere.org.
License
Copyright 2025 Rodolfo Berrios A.
Chevere is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.