scaleplan/http-status

此软件包最新版本(3.0.0)没有提供许可信息。

一个用于处理HTTP状态的最小软件包。

3.0.0 2019-03-27 00:52 UTC

This package is auto-updated.

Last update: 2024-09-16 19:54:06 UTC


README

Latest Version on Packagist Software License Build Status Build Status Total Downloads

Httpstatus软件包提供了一个简单且方便的方法来检索任何给定HTTP状态代码的标准状态文本(英文)。您还可以获取任何有效状态文本的HTTP状态代码。此外,此软件包还提供了所有状态代码作为常量,以使您的代码可读性更好(HTTP_OK 比起 200 更容易理解)。

安装

通过 Composer

$ composer require lukasoppermann/http-status

用法

$Httpstatus = new Lukasoppermann\Httpstatus\Httpstatus();

// 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[链接]

状态代码的第一个数字定义了响应的类别。最后两个数字没有分类作用。第一个数字有五个值

可用的HTTP状态代码

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

测试

$ composer test

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全

如果您发现任何安全相关的问题,请通过电子邮件 oppermann.lukas@gmail.com 而不是使用问题跟踪器。

致谢

许可

MIT许可(MIT)。请参阅 许可文件 了解更多信息。