lukasoppermann / http-status
一个用于处理HTTP状态的简单包。
4.0.0
2023-06-23 18:39 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- league/csv: ^7.1
- php-coveralls/php-coveralls: ^2.4
- phpunit/phpunit: ~9.0
README
Httpstatus包提供了一种简单方便的方式,用于检索任何给定HTTP状态码的标准状态文本(英文)。您还可以获取任何有效状态文本的HTTP状态码。此外,此包还提供了所有状态码作为常量,以更好地提高代码的可读性(《HTTP_OK》比《200》更容易理解)。
安装
通过Composer
$ composer require lukasoppermann/http-status
用法
$Httpstatus = new Lukasoppermann\Httpstatus\Httpstatus(); // (optional) specify language, default: en $Httpstatus->setLanguage('en'); // Currently supported: en, fr // get status text from code echo $Httpstatus->getReasonPhrase(301); // Moved Permanently // get the status code by text echo $Httpstatus->getStatusCode('Method Not Allowed'); // 405 // check if status code exists echo $Httpstatus->hasStatusCode(404); // true echo $Httpstatus->hasStatusCode(601); // false // check if reason phrase exists echo $Httpstatus->hasReasonPhrase('Method Not Allowed'); // true echo $Httpstatus->hasReasonPhrase('Does not exist'); // false // determine the type (or "class") of the code echo $Httpstatus->getResponseClass(503); // Httpstatus::CLASS_SERVER_ERROR
此包提供了一个接口,其中包含所有状态码作为常量,以便您方便使用。当开发处理HTTP状态码的类时,只需实现接口,开始使用常量而不是魔法数字,以编写更易于阅读和理解的可读代码。
use Lukasoppermann\Httpstatus\Httpstatuscodes; class Response implements Httpstatuscodes{ public function someMethod(){ // ... some logic return respond(self::HTTP_CREATED, $json); } }
如果您愿意,也可以直接使用接口中的常量。
use Lukasoppermann\Httpstatus\Httpstatuscodes as Status; class UserTest{ public function test_create_new_user(){ $this->assertEquals(Status::HTTP_CREATED, $response->status()); } }
配置
如果您想本地化状态文本,可以在初始化类时提供一个数组。您可以覆盖所有代码或仅覆盖一些代码。原因短语必须是唯一的,并且只能用于一个状态码。
// add custom texts $Httpstatus = new Lukasoppermann\Httpstatus\Httpstatus([ 200 => 'Kein Inhalt', 404 => 'Nicht gefunden', ]);
HTTP状态码类(《来自RFC7231的部分6》[https://tools.ietf.org/html/rfc7231#section-6])
状态码的第一位数字定义了响应的类别。最后两位数字不具有任何分类作用。第一位数字有五个值
可用的HTTP状态码
变更日志
请参阅变更日志以获取更多最近更改的信息。
测试
$ composer test
贡献
请参阅贡献指南以获取详细信息。
安全性
如果您发现任何安全相关的问题,请通过电子邮件oppermann.lukas@gmail.com联系,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。