bear/accept
Composer 安装命令:
composer require bear/accept
包简介
Provides content-negotiation using Accept* headers for BEAR.Sunday
README 文档
README
Provides content-negotiation using Accept* headers for BEAR.Sunday
Composer install
$ composer require bear/accept
Module install
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-JPmatches, 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].
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
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.
+ /* @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.
use BEAR\Sunday\Module\Constant\NamedModule; class JaModule extends AbstractModule { /** * {@inheritdoc} */ protected function configure() { $text = ['greeting' => 'こんにちは']; $this->install(new NamedModule($text)); } }
bear/accept 适用场景与选型建议
bear/accept 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 76.59k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「content negotiation」 「accept」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bear/accept 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bear/accept 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bear/accept 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A block that displays featured content - large image, title, description and link.
A bundle that creates REST Apis based on your current controllers actions
PHP parser for HTTP Accept, Accept-Language, Accept-Encoding, and Content-Type headers.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
Portal Options Links
Library for easily converting reports into various HTTP responses
统计信息
- 总下载量: 76.59k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-09