snicco/content-negotiation-middleware

v1.9.0 2023-09-20 12:57 UTC

README

codecov Psalm Type-Coverage Psalm level PhpMetrics - Static Analysis PHP-Versions

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.

Installation

composer require snicco/content-negotiation-middleware

Usage

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,
            ],
   ] 
);

Contributing

This repository is a read-only split of the development repo of the Snicco project.

This is how you can contribute.

Reporting issues and sending pull requests

Please report issues in the Snicco monorepo.

Security

If you discover a security vulnerability, please follow our disclosure procedure.