Content negotiation middleware for snicco/http-routing
A middleware that uses middlewares/negotiation
with some sensible
defaults.
It will determine the best content-type and content-language based on the provided Accept
headers and will update
the Accept
header of the request accordingly so that it only contains one content-type/content-language.
If no matching content type can be determined a 406 HTTPException
is thrown.
composer require snicco/content-negotiation-middleware
This middleware should be added globally in the MiddlewareResolver
.
It must be bound in the PSR-11 container that the snicco/http-routing
component uses.
// In your container definitions
use Snicco\Middleware\Negotiation\NegotiateContent;
// basic configuration based on defaults.
$negotiation = new NegotiateContent(
['en'] // content languages
);
// With custom configuration for content-types your application can provide (sorted by priority)
$negotiation = new NegotiateContent(
['en'],
[
'html' => [
'extension' => ['html', 'php'],
'mime-type' => ['text/html'],
'charset' => true,
],
'txt' => [
'extension' => ['txt'],
'mime-type' => ['text/plain'],
'charset' => true,
],
'json' => [
'extension' => ['json'],
'mime-type' => ['application/json'],
'charset' => true,
],
]
);
This repository is a read-only split of the development repo of the Snicco project.
This is how you can contribute.
Please report issues in the Snicco monorepo.
If you discover a security vulnerability, please follow our disclosure procedure.