vperyod / accept-handler
PSR7 Aura\Accept Handler
0.1.0
2016-06-20 16:40 UTC
Requires
- aura/accept: ^2.0
- psr/http-message: ^1.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-10-29 04:25:33 UTC
README
Aura\Accept Content Negotiation Middleware
Installation
composer require vperyod/accept-handler
Usage
See Aura\Accept documentation.
// Create handler $handler = new Vperyod\AcceptHandler\AcceptHandler(); // Optionally set the attribute on which to store the `Accept` object // Defaults to 'aura/accept:accept' $handler->setAcceptAttribute('accept'); // Add to your middleware stack, radar, relay, etc. $stack->middleware($handler); // Subsequent dealings with `Request` will have the `Accept` instance available // at the previous specified atribute $accept = $request->getAttribute('accept'); // The `AcceptRequestAwareTrait` should make dealings easier. // // Have all your objects that deal with the accept attribute on the request use // the `AcceptRequestAwareTrait` and have your DI container use the setter, so that // they all know where the Accept object is stored. // // Additionally, the trait supplies negotiate methods to eaily access the the // `Accept` Negotiation methods. class MyResponder { use \Vperyod\AcceptHandler\AcceptRequestAwareTrait; protected $availableLangs = [ //... ]; protected $availableCharset = [ //... ]; protected $availableMedia = [ //... ]; public function __invoke($request, $response, $payload) { // get the accept object $accept = $this->getAccept($request); // or more convieniant methods $language = $this->negotiateLanguage($request, $this->availableLangs); $charset = $this->negotiateCharset($request, $this->availableCharset) $media = $this->negotiateMedia($request, $this->availableMedia); //... } }