{"id":13712053,"url":"https://github.com/middlewares/negotiation","last_synced_at":"2026-01-07T23:54:54.449Z","repository":{"id":56001255,"uuid":"69752287","full_name":"middlewares/negotiation","owner":"middlewares","description":"PSR-15 middleware to implement content negotiation","archived":false,"fork":false,"pushed_at":"2025-03-26T14:50:49.000Z","size":75,"stargazers_count":45,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-24T07:24:59.759Z","etag":null,"topics":["content-negotiation","http","middleware","negotiation","psr-15"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/middlewares.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-01T17:19:04.000Z","updated_at":"2025-03-22T19:59:31.000Z","dependencies_parsed_at":"2024-06-18T16:43:52.435Z","dependency_job_id":"3eafb5af-c370-4e88-bcaa-928aa4d0b814","html_url":"https://github.com/middlewares/negotiation","commit_stats":{"total_commits":75,"total_committers":5,"mean_commits":15.0,"dds":0.09333333333333338,"last_synced_commit":"d2d44ea744109216ef9569653c179b2005c77a85"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Fnegotiation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Fnegotiation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Fnegotiation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Fnegotiation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/middlewares","download_url":"https://codeload.github.com/middlewares/negotiation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252772269,"owners_count":21801894,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["content-negotiation","http","middleware","negotiation","psr-15"],"created_at":"2024-08-02T23:01:14.435Z","updated_at":"2026-01-07T23:54:54.417Z","avatar_url":"https://github.com/middlewares.png","language":"PHP","readme":"# middlewares/negotiation\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]](LICENSE)\n![Testing][ico-ga]\n[![Total Downloads][ico-downloads]][link-downloads]\n\nMiddleware using [wildurand/Negotiation](https://github.com/willdurand/Negotiation) to implement content negotiation. Contains the following components:\n\n* [ContentType](#contenttype)\n* [ContentLanguage](#contentlanguage)\n* [ContentEncoding](#contentencoding)\n\n## Requirements\n\n* PHP \u003e= 7.2\n* A [PSR-7 http library](https://github.com/middlewares/awesome-psr15-middlewares#psr-7-implementations)\n* A [PSR-15 middleware dispatcher](https://github.com/middlewares/awesome-psr15-middlewares#dispatcher)\n\n## Installation\n\nThis package is installable and autoloadable via Composer as [middlewares/negotiation](https://packagist.org/packages/middlewares/negotiation).\n\n```sh\ncomposer require middlewares/negotiation\n```\n\n## Example\n\n```php\nDispatcher::run([\n    new Middlewares\\ContentType(),\n    new Middlewares\\ContentLanguage(['en', 'gl', 'es']),\n    new Middlewares\\ContentEncoding(['gzip', 'deflate']),\n]);\n```\n\n## ContentType\n\nTo detect the preferred mime type using the `Accept` header and the file extension and edit the header with this value. A `Content-Type` header is also added to the response if it's missing.\n\nDefine the formats to negotiate sorted by priority in the first argument. By default uses [these](src/formats_defaults.php)\n\n```php\n//Use the default types\n$negotiator = new Middlewares\\ContentType();\n\n//Use only few types\n$negotiator = new Middlewares\\ContentType(['html', 'json']);\n\n//Use only few types and configure some of them\n$negotiator = new Middlewares\\ContentType([\n    'html',\n    'json',\n    'txt' =\u003e [\n        'extension' =\u003e ['txt'],\n        'mime-type' =\u003e ['text/plain'],\n        'charset' =\u003e true,\n    ]\n]);\n```\n\n### errorResponse\n\nIf no format matches the negotiation, by default the middleware use the first value in the list of available formats (by default `text/html`). Use this option to return a `406` error. Optionally, you can provide a `Psr\\Http\\Message\\ResponseFactoryInterface` that will be used to create the response. If it's not defined, [Middleware\\Utils\\Factory](https://github.com/middlewares/utils#factory) will be used to detect it automatically.\n\n```php\n$responseFactory = new MyOwnResponseFactory();\n\n//Use default html format (the first provided) if no valid format was detected (By default)\n$negotiator = new Middlewares\\ContentType(['html', 'json']);\n\n//Return a 406 response if no valid format was detected\n$negotiator = (new Middlewares\\ContentType(['html', 'json']))-\u003eerrorResponse();\n\n//Return a 406 response using a specific responseFactory if no valid format was detected\n$negotiator = (new Middlewares\\ContentType(['html', 'json']))-\u003eerrorResponse($responseFactory);\n```\n\n### charsets\n\nThe available charsets to negotiate with the `Accept-Charset` header. By default is `UTF-8`.\n\n```php\n$negotiator = (new Middlewares\\ContentType())-\u003echarsets(['UTF-8', 'ISO-8859-1']);\n```\n\n### noSniff\n\nAdds the `X-Content-Type-Options: nosniff` header, to mitigating [MIME confusión attacks.](https://blog.mozilla.org/security/2016/08/26/mitigating-mime-confusion-attacks-in-firefox/). Enabled by default.\n\n```php\n//Disable noSniff header\n$negotiator = (new Middlewares\\ContentType())-\u003enoSniff(false);\n```\n\n### attribute\n\nTo store the format name (`json`, `html`, `css` etc) in an attribute of the `ServerRequest`.\n\n## ContentLanguage\n\nTo detect the preferred language using the `Accept-Language` header or the path prefix and edit the header with this value. A `Content-Language` header is also added to the response if it's missing.\n\nThe first argument is an array with the available languages to negotiate sorted by priority. The first value will be used as default if no other languages is choosen in the negotiation.\n\n```php\n$request = Factory::createServerRequest('GET', '/')\n    -\u003ewithHeader('Accept-Language', 'gl-es, es;q=0.8, en;q=0.7');\n\nDispatcher::run([\n    new Middlewares\\ContentLanguage(['es', 'en']),\n\n    function ($request) {\n        $language = $request-\u003egetHeaderLine('Accept-Language');\n\n        switch ($language) {\n            case 'es':\n                return 'Hola mundo';\n            case 'en':\n                return 'Hello world';\n        }\n    }\n], $request);\n```\n\n### usePath\n\nBy enabling this option, the base path will be used to detect the language. This is useful if you have different paths for each language, for example `/gl/foo` and `/en/foo`. \n\nNote: the language in the path has preference over the `Accept-Language` header.\n\n```php\n$request = Factory::createServerRequest('GET', '/en/hello-world');\n\nDispatcher::run([\n    (new Middlewares\\ContentLanguage(['es', 'en']))-\u003eusePath(),\n\n    function ($request) {\n        $language = $request-\u003egetHeaderLine('Accept-Language');\n\n        switch ($language) {\n            case 'es':\n                return 'Hola mundo';\n            case 'en':\n                return 'Hello world';\n        }\n    }\n], $request);\n```\n\n### redirect\n\nUsed to return a `302` responses redirecting to the path containing the language. This only works if `usePath` is enabled, so for example, if the request uri is `/welcome`, returns a redirection to `/en/welcome`.\n\n```php\n$responseFactory = new MyOwnResponseFactory();\n\n//Use not only the Accept-Language header but also the path prefix to detect the language\n$negotiator = (new Middlewares\\ContentLanguage(['es', 'en']))-\u003eusePath();\n\n//Returns a redirection with the language in the path if it's missing\n$negotiator = (new Middlewares\\ContentLanguage(['es', 'en']))-\u003eusePath()-\u003eredirect();\n\n//Returns a redirection using a specific response factory\n$negotiator = (new Middlewares\\ContentLanguage(['es', 'en']))-\u003eusePath()-\u003eredirect($responseFactory);\n```\n\n## ContentEncoding\n\nTo detect the preferred encoding type using the `Accept-Encoding` header and edit the header with this value.\n\n```php\n$request = Factory::createServerRequest('GET', '/')\n    -\u003ewithHeader('Accept-Encoding', 'gzip,deflate');\n\nDispatcher::run([\n    new Middlewares\\ContentEncoding(['gzip']),\n\n    function ($request) {\n        echo $request-\u003egetHeaderLine('Accept-Encoding'); //gzip\n    }\n], $request);\n```\n\n---\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.\n\nThe MIT License (MIT). Please see [LICENSE](LICENSE) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/middlewares/negotiation.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-ga]: https://github.com/middlewares/negotiation/workflows/testing/badge.svg\n[ico-downloads]: https://img.shields.io/packagist/dt/middlewares/negotiation.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/middlewares/negotiation\n[link-downloads]: https://packagist.org/packages/middlewares/negotiation\n","funding_links":[],"categories":["Packages"],"sub_categories":["Content"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiddlewares%2Fnegotiation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiddlewares%2Fnegotiation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiddlewares%2Fnegotiation/lists"}