shrikeh/teapot
Composer 安装命令:
composer require shrikeh/teapot
包简介
PHP HTTP Response Status library
关键字:
README 文档
README
This is a very simple library that aims to aid verbosity in any Web-based application by defining clearly the HTTP 1.1 response codes as constants. It includes two main components: an interface, which contains the constants, and an exception specifically for HTTP.
Usage
Using the StatusCodes interface
Assuming for a moment a PHPUnit test on a cURL client response:
<?php /** * @dataProvider someUrlProvider */ public function testResponseIsOK($url) { $client = new Client($url); $response = $client->get(); $this->assertSame(200, $response->getStatusCode()); }
This becomes:
<?php use Teapot\StatusCode; ... $this->assertSame(StatusCode::OK, $response->getStatusCode());
While this is a trivial example, the additional verbosity of the code is clearer with other HTTP status codes:
<?php use Teapot\StatusCode; $code = $response->getStatusCode(); $this->assertNotEquals(StatusCode::NOT_FOUND, $code); $this->assertNotEquals(StatusCode::FORBIDDEN, $code); $this->assertNotEquals(StatusCode::MOVED_PERMANENTLY, $code); $this->assertSame(StatusCode::CREATED, $code);
As StatusCode is an interface without any methods, you can directly implement it if you prefer:
<?php use Teapot\StatusCode; class FooController implements StatusCode { public function badAction() { if ($this->request->getMethod() == 'POST') { throw new \Exception('Bad!', self::METHOD_NOT_ALLOWED); } } }
This may be beneficial in an abstract class, so that child classes don't need to explicitly use the interface.
There are various "helper" interfaces within the library, such as WebDAV and Http. Additionally, the various status codes are split into the RFCs that defined them: the Http helper interface extends RFCs 2616, 2324, and 2774, for example. This allows you very granular control of what status codes you want to allow within your application.
All constants have doc blocks that use the official W3C and IETF draft specification descriptions of the status code, to aid IDEs and for reference.
Using the HttpException
The HttpException is very straightforward. It simply is a named exception to aid verbosity:
<?php use Teapot\HttpException; use Teapot\StatusCode; throw new HttpException( 'Sorry this page does not exist!', StatusCode::NOT_FOUND );
The exception itself uses the StatusCode interface, allowing you to avoid manually and explicitly importing it if you prefer:
<?php use Teapot\HttpException; throw new HttpException( 'Sorry this page does not exist!', HttpException::NOT_FOUND );
Installation
Run the following command.
composer require shrikeh/teapot
Coding Standards
The entire library is intended to be PSR-1, PSR-2 and PSR-4 compliant.
Get in touch
If you have any suggestions, feel free to email me at barney+teapot@shrikeh.net or ping me on Twitter with @shrikeh.
shrikeh/teapot 适用场景与选型建议
shrikeh/teapot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.46M 次下载、GitHub Stars 达 274, 最近一次更新时间为 2013 年 03 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 shrikeh/teapot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shrikeh/teapot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 shrikeh/teapot 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
http客户端
Easily add Excepct-CT header to your project
Build mini web servers in PHP for testing
Cbox SSRF — a hardened, config-driven guard against server-side request forgery for outbound URLs in Laravel. Blocks private/reserved/cloud-metadata targets, pins DNS, and refuses redirects.
Eventually this library may have some useful HTTP-related helpers, but currently there's only HTTP response status codes.
统计信息
- 总下载量: 2.46M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 278
- 点击次数: 23
- 依赖项目数: 44
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2013-03-27