avris / http
HTTP Request/Response abstraction
Requires
- php: ^7.1
- avris/bag: ^4.0
Requires (Dev)
- avris/function-mock: ^1.0
- phpunit/phpunit: ^6.5
- squizlabs/php_codesniffer: ^3.2
- symfony/var-dumper: ^4.0
This package is not auto-updated.
Last update: 2024-12-19 23:55:37 UTC
README
HTTP Request/Response abstraction
Instalation
composer require avris/http
Usage
Request
What PHP puts in ugly superglobals $_SERVER
, $_GET
, $_POST
, $_COOKIE
and $_FILES
,
the Request
class encapsulates in a nice object. Just do:
$request = Request::fromGlobals();
and you'll be able to elegantly access all the data and pass it along.
getMethod(): string
isPost(): bool
getScheme(): string
getHost(): string
getAbsoluteBase(): string
getBase(): string
getFrontController(): string
getUrl(): string
getCleanUrl(): string
getFullUrl(): string
getIp(bool $safe = true): string
isAjax(): bool
getQuery(): Bag
--$_GET
getData(): Bag
--$_POST
getCookies(): Bag
getServer(): Bag
getFiles(): FileBag
getHeaders(): HeaderBag
getBody(): string
getJsonBody()
For purposes of testing etc. you can also create a request object with some arbitrary data, for instance:
$request = Request::create('DELETE', '/posts/123', [], ['redirect' => '1']);
The abstraction layer for headers provides additional features,
like getting the best Accept-*
value.
Files in the request will be wrapped in an UploadedFile
:
if ($request->isPost()) {
$file = $request->getFiles()->get('attachment');
$filename = $file->moveToRandom(__DIR__ . '/pics');
$post->setAttachment($filename);
}
Response
In a similar fashon, the Response
class encapsulates an HTTP Response.
Your controller might for instance return:
return new Response(
'<strong>OK</strong>',
201,
['Content-Type' => 'text/html; charset=utf-8'],
new CookieBag([
new Cookie('foo', 'bar', new \DateTime('+10 minutes')),
]),
);
This object can then be passed on, modified, extended etc., without actually sending any data over HTTP.
Only after you execute $response->send()
, your headers, cookies, response code and content will be sent.
The library provides some nice helpers for creating more standard responses:
* `new JsonResponse(['foo' => bar])`
* `new RedirectResponse('/done')`
* `new FileResponse($post->getAttachmentPath(), 'image.png')`
* `new MemoryFileResponse($image->getContent())`
* `new StreamedResponse(function() { while (true) { echo 'heartbeat' . PHP_EOL; sleep(1); }})`
Session
Instead of using the $_SESSION
superglobal, you can encapsulate it in a Session
object.
It works basically the same, but lets you write more elegant, object oriented, testable code,
as well as easily exchange the standard session with a different one.
StoragelessSession
is an alternative approach to standard HTTP sessions.
It doesn't story any information on the server, instead sends it back and forth using cookies.
(Therefore it should only be used for small amount of non-confidential data over a secure connection!)
$session = new StoragelessSession;
// ...
$session->onRequest($request);
// ...
$foo = $session->get('foo');
$session->set('bar', $bar);
// ...
$session->onResponse($response);
// ...
$response->send();
Copyright
- Author: Andre Prusinowski (Avris.it)
- Licence: MIT