Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bearsunday/bear.accept
Provides content-negotiation using `Accept` headers for BEAR.Sunday
https://github.com/bearsunday/bear.accept
bearsunday bearsunday-module content-negotiation
Last synced: 30 days ago
JSON representation
Provides content-negotiation using `Accept` headers for BEAR.Sunday
- Host: GitHub
- URL: https://github.com/bearsunday/bear.accept
- Owner: bearsunday
- License: mit
- Created: 2017-08-09T15:20:13.000Z (over 7 years ago)
- Default Branch: 1.x
- Last Pushed: 2024-01-11T01:38:09.000Z (12 months ago)
- Last Synced: 2024-11-26T09:15:46.761Z (about 1 month ago)
- Topics: bearsunday, bearsunday-module, content-negotiation
- Language: PHP
- Homepage:
- Size: 79.1 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# BEAR.Accept
Provides content-negotiation using Accept* headers for [BEAR.Sunday](http://bearsunday.github.io/)
## Composer install
$ composer require bear/accept
## Module install```php
use Ray\Di\AbstractModule;
use Ray\Di\Scope;class AppModule extends AbstractModule
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$available = [
'Accept' => [
'application/hal+json' => 'prod-hal-app',
'application/json' => 'prod-app',
'text/csv' => 'prod-csv-app',
'cli' => 'prod-html-app'
],
'Accept-Language' => [
'en-US' => 'en',
'ja-JP' => 'ja'
]
];
// $available support 'Accept' and 'Accept-Language' key only
$this->install(new AcceptModule($available));
}
}
````Accept` specifies all of the available media in the format `[$mediatype => $context]`. `cli` is the context in case of console access. The renderer of the context of the media type matched by content negotiation is used for rendering the resource.
`Accept-Language` specifies all available languages in the format `[$lang => $contextKey]`.
For example, if `application/hal+json` and `ja-JP`matches, the `$context` is `prod-hal-jp-app`. (We set `JpModule` in `App\Module` folder and bind it for Japanese.)
# Usage
## Apply to the specified resource
Annotate the resource to do content negotiation with `@Produces`.
```php
use use BEAR\Accept\Annotation\Produces;/**
* @Produces({"application/json", "text/csv"})
*/
public function onGet()
```**application/json** and **text/csv** media type is available for this resource. The `Vary` header is added automatically.
## Apply to all resources
To perform content negotiation on all resources, prepare a special bootstrap file. This is especially useful when negotiating languages.
cn.php
```php
require dirname(__DIR__) . '/vendor/autoload.php';$available = [
'Accept' => [
'text/html' => 'prod-html-app',
'application/hal+json' => 'prod-hal-app',
'application/json' => 'prod-app',
'cli' => 'cli-html-app'
],
'Accept-Language' => [
'ja' => 'ja',
'en-US' => 'us'
]
];
$accept = new \BEAR\Accept\Accept($available);
[$context, $vary] = $accept($_SERVER);
//```
Add a vary header in `Bootstrap` to enable caching when using content negotiation.
```diff
+ /* @global \BEAR\Resource\Vary $vary */
+ if (isset($vary)) {
+ $page->headers['Vary'] = $vary;
+ }
$page->transfer($app->responder, $_SERVER);
```Prepare the module of the DI setting necessary for each language.
```php
use BEAR\Sunday\Module\Constant\NamedModule;class JaModule extends AbstractModule
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$text = ['greeting' => 'こんにちは'];
$this->install(new NamedModule($text));
}
}
```