定制 ptlis/psr7-conneg 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ptlis/psr7-conneg

最新稳定版本:v1.2.0

Composer 安装命令:

composer require ptlis/psr7-conneg

包简介

PSR7 content negotiation

README 文档

README

A content negotiation middleware that uses the PSR-7 interfaces.

Configurable component that decorate processed requests with preferred type information.

Built upon ptlis/ConNeg.

Build Status Code Coverage Scrutinizer Code Quality License Latest Stable Version

Install

Either from the console:

    $ composer require ptlis/psr7-conneg:~1.2

Or by Editing composer.json:

    {
        "require": {
            ...
            "ptlis/conneg-psr7-conneg": "~1.2",
            ...
        }
    }

Followed by a composer update:

    $ composer update

Usage

The package ships with a single class to provide negotiation.

    use ptlis\Psr7ConNeg\Negotiator;
    
    $negotiator = new Negotiator();

To opt-in to negotiation on a field use the appropriate with* method (note these methods return a new instance in the manner of the PSR-7 interfaces).

To negotiate the preferred mime-type use the withMime method, providing it with a list of your application's type preference:

    $negotiator = $negotiator->withMime('application/json;q=1.0,text/xml;q=0.7');

With your negotiator configured you can now perform negotiation:

    $request = $negotiator->negotiate($request);

This adds attributes to the request containing the preferred type. These can be accessed with the appropriate getters, in the above example of negotiation on the Accept field this looks like:

    $mime = $newRequest->getAttribute(Negotiator::MIME_BEST);

If the Accept field of the request contained application/json,text/xml then the value returned from this lookup would be application/json.

With Zend-Stratigility

To use the zend-stratigility component, configure your negotiator instance as described above and pass it to the Stratigility negotiator.

    $stratigilityNegotiator = new StratigilityNegotiator($negotiator);

To perform negotiation on all routes, add it with an empty route:

    $app->pipe('', $stratigilityNegotiator);

For further information please refer to the Stratigility documentation.

With Relay

To use the relay component, configure your negotiator instance as described above and pass it to the Relay negotiator.

    $queue[] = new RelayNegotiator($negotiator);

Note: The negotiator must be earlier in the queue than middleware components that depend upon negotiation data.

Integration

Middlewares integration is shipped for the following packages:

Contributing

You can contribute by submitting an Issue to the issue tracker or submitting a pull request.

统计信息

  • 总下载量: 37
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-12