neat / http
Neat HTTP components
Installs: 3 217
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 1
Requires
- php: >=7.0
- psr/http-message: ^1.0
Requires (Dev)
- guzzlehttp/psr7: ^1.6
- phpunit/phpunit: ^6.5 || ^7.0 || ^8.0 !=8.5.0
Suggests
- neat/http-client: Neat HTTP client components
- neat/http-server: Neat HTTP server components
README
Neat HTTP components provide a clean and expressive API for your application to access HTTP messages.
Requirements
To use Neat HTTP components you will need
- PHP 7.0 or newer
- a PSR-7 HTTP message implementation
To send and receive messages, we suggest using the neat/http-client and neat/http-server packages.
Getting started
To install this package, simply issue composer on the command line:
composer require neat/http
Reading the request
The request can be read using simple methods like shown below.
<?php /** @var Neat\Http\ServerRequest $request */ // Get ?page= query parameter $page = $request->query('page'); // Get posted name field $name = $request->post('name'); // Or just get all posted fields $post = $request->post(); // Who doesn't want a cookie $preference = $request->cookie('preference'); // Get the request method $method = $request->method(); // And of course the requested URL $url = $request->url();
URL inspection
To save you the trouble of dissecting and concatenating URL's by hand, the URL class will lend you a hand:
<?php /** @var Neat\Http\Url $url */ // You can easily print the URL because it converts to a string when needed echo $url; // Or get parts of the url separately $url->scheme(); // 'https' $url->username(); // '' $url->password(); // '' $url->host(); // 'example.com' $url->port(); // null $url->path(); // '/articles' $url->query(); // 'page=2' $url->fragment(); // '' // Do you want to create a slightly different URL based on this one? $mutation = $url->withPath('/just')->withQuery('tweak=any&part=youlike');
File uploads
Uploaded files can be accessed through the request using the file
method.
<?php /** @var Neat\Http\ServerRequest $request */ // Get uploaded file with the name avatar $file = $request->files('avatar'); // Check the error status if (!$file->ok()) { echo 'Upload error id = ' . $file->error(); } // Get the file name, size and mime type $file->clientName(); // 'selfie.jpg' <-- provided by the client, so consider it unsafe user input $file->clientType(); // 'image/jpeg' <-- provided by the client, so consider it unsafe user input $file->size(); // 21359 // Move the file from its temporary location $file->moveTo('destination/path/including/filename.jpg');