libero / content-negotiation-bundle
Adds content negotiation to your Symfony application
Installs: 683
Dependents: 1
Suggesters: 1
Security: 0
Stars: 0
Watchers: 4
Forks: 1
Type:symfony-bundle
Requires
- php: ^7.2
- symfony/config: ^3.4 || ^4.0
- symfony/dependency-injection: ^3.4 || ^4.0
- symfony/http-foundation: ^3.4 || ^4.0
- symfony/http-kernel: ^3.4 || ^4.0
- symfony/routing: ^3.4 || ^4.0
- willdurand/negotiation: ^2.3
Requires (Dev)
- libero/coding-standard: ^0.3
- phpstan/phpstan: ^0.10
- phpstan/phpstan-phpunit: ^0.10
- phpunit/phpunit: ^7.2
- psr/log: ^1.0
- symfony/filesystem: ^3.4 || ^4.0
- symfony/framework-bundle: ^3.4 || ^4.0
This package is auto-updated.
Last update: 2020-02-07 21:07:44 UTC
README
This is a Symfony bundle that will add content negotiation to your application by integrating the Negotiation library.
Getting started
Using Composer you can add the bundle as a dependency:
composer require libero/content-negotation-bundle
If you're not using Symfony Flex, you'll need to enable the bundle in your application.
Path-based negotiation
You can add negotiation to paths through configuration.
For example, to add a requirement of XML or JSON and English or French to an exact path, and plain text and German elsewhere:
content_negotiation: formats: - path: ^/path/to/my/page$ priorities: xml|json - path: ^/ priorities: txt locales: - path: ^/path/to/my/page$ priorities: en|fr - path: ^/ priorities: de
The formats
and locales
items are run in order. The first to match will be used.
priorities
may be empty, allowing for negotiation to be disabled at lower levels. For example, require English everywhere except under /foo
:
content_negotiation: formats: - path: ^/foo($|/) priorities: - path: ^/ priorities: en
optional
may be set to true
to allow falling back to subsequent matches. For example, to require English everywhere except under /foo
, where either German or English is allowed:
content_negotiation: formats: - path: ^/foo($|/) priorities: de optional: true - path: ^/ priorities: en
Route-level negotiation
You can add negotiation at the route level by adding requirements for _format
and/or _locale
.
These requirements must be a list of possibilities separated by vertical bars.
For example, to add a requirement of XML or JSON and English or French to a route:
my_route: path: /path/to/my/page controller: App\Controller\PageController requirements: _format: xml|json _locale: en|fr
Route-level negotiation takes precedence over path-based.
Getting help
- Report a bug or request a feature on GitHub.
- Ask a question on the Libero Community Slack.
- Read the code of conduct.