ctw /ctw-http
此包提供了工具类和常量,以简化PSR-7的常见操作,以及表示HTTP状态码的一组异常。
4.0.0
2024-06-18 06:01 UTC
Requires
- php: ^8.3
- fig/http-message-util: ^1.1
Requires (Dev)
- ctw/ctw-qa: ^4.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.7
- phpstan/phpstan-phpunit: ^1.1
- phpstan/phpstan-strict-rules: ^1.3
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.6
- symfony/var-dumper: ^7.0
This package is auto-updated.
Last update: 2024-09-18 08:47:36 UTC
README
此仓库包含工具类和常量,以简化PSR-7的常见操作和表示HTTP状态码的一组异常。
安装
通过将包添加为Composer依赖项进行安装
$ composer require ctw/ctw-http
使用示例
返回实体
use Ctw\Http\HttpStatus; $httpStatus = new HttpStatus(HttpStatus::STATUS_NOT_FOUND); dump($httpStatus->get());
^ Ctw\Http\Entity\HttpStatus^ {#2 +statusCode: 404 +name: "Not Found" +phrase: "The requested resource could not be found but may be available again in the future." +exception: "Ctw\Http\HttpException\NotFoundException" +url: "https://httpstatuses.com/404" }
抛出异常
HttpException\*
异常
use Ctw\Http\HttpException; throw new HttpException\NotFoundException();
PHP Fatal error: Uncaught Ctw\Http\HttpException\NotFoundException: 404 Not Found in /path/demo-usage.php:20 Stack trace: #0 {main} thrown in /path/demo-usage.php on line 20
带有自定义错误消息的HttpException\*
异常
use Ctw\Http\HttpException; throw new HttpException\NotFoundException('Custom 404 error message');
PHP Fatal error: Uncaught Ctw\Http\HttpException\NotFoundException: Custom 404 error message in /path/demo-usage.php:21 Stack trace: #0 {main} thrown in /path/demo-usage.php on line 21
捕获异常
所有HttpException\*
异常都实现了HttpException\HttpExceptionInterface
,因此
use Ctw\Http\HttpException; try { throw new HttpException\NotFoundException(); } catch (HttpException\HttpExceptionInterface $e) { dump($e->getStatusCode()); dump($e->getMessage()); }
HttpException\*
层次结构如下
use Ctw\Http\HttpException\HttpExceptionInterface; use Ctw\Http\HttpException\AbstractException; use Ctw\Http\HttpException\AbstractClientErrorException; use Ctw\Http\HttpException\AbstractServerErrorException; HttpExceptionInterface └── AbstractException ├── AbstractClientErrorException │ └── *Exception └── AbstractServerErrorException └── *Exception
HTTP方法常量列表
use Ctw\Http\HttpMethod; HttpMethod::METHOD_CONNECT; HttpMethod::METHOD_DELETE; HttpMethod::METHOD_GET; HttpMethod::METHOD_HEAD; HttpMethod::METHOD_OPTIONS; HttpMethod::METHOD_PATCH; HttpMethod::METHOD_POST; HttpMethod::METHOD_PURGE; HttpMethod::METHOD_PUT; HttpMethod::METHOD_TRACE;
HTTP状态常量列表
use Ctw\Http\HttpStatus; HttpStatus::STATUS_CONTINUE; HttpStatus::STATUS_SWITCHING_PROTOCOLS; HttpStatus::STATUS_PROCESSING; HttpStatus::STATUS_EARLY_HINTS; HttpStatus::STATUS_OK; HttpStatus::STATUS_CREATED; HttpStatus::STATUS_ACCEPTED; HttpStatus::STATUS_NON_AUTHORITATIVE_INFORMATION; HttpStatus::STATUS_NO_CONTENT; HttpStatus::STATUS_RESET_CONTENT; HttpStatus::STATUS_PARTIAL_CONTENT; HttpStatus::STATUS_MULTI_STATUS; HttpStatus::STATUS_ALREADY_REPORTED; HttpStatus::STATUS_IM_USED; HttpStatus::STATUS_MULTIPLE_CHOICES; HttpStatus::STATUS_MOVED_PERMANENTLY; HttpStatus::STATUS_FOUND; HttpStatus::STATUS_SEE_OTHER; HttpStatus::STATUS_NOT_MODIFIED; HttpStatus::STATUS_USE_PROXY; HttpStatus::STATUS_RESERVED; HttpStatus::STATUS_TEMPORARY_REDIRECT; HttpStatus::STATUS_PERMANENT_REDIRECT; HttpStatus::STATUS_BAD_REQUEST; HttpStatus::STATUS_UNAUTHORIZED; HttpStatus::STATUS_PAYMENT_REQUIRED; HttpStatus::STATUS_FORBIDDEN; HttpStatus::STATUS_NOT_FOUND; HttpStatus::STATUS_METHOD_NOT_ALLOWED; HttpStatus::STATUS_NOT_ACCEPTABLE; HttpStatus::STATUS_PROXY_AUTHENTICATION_REQUIRED; HttpStatus::STATUS_REQUEST_TIMEOUT; HttpStatus::STATUS_CONFLICT; HttpStatus::STATUS_GONE; HttpStatus::STATUS_LENGTH_REQUIRED; HttpStatus::STATUS_PRECONDITION_FAILED; HttpStatus::STATUS_PAYLOAD_TOO_LARGE; HttpStatus::STATUS_URI_TOO_LONG; HttpStatus::STATUS_UNSUPPORTED_MEDIA_TYPE; HttpStatus::STATUS_RANGE_NOT_SATISFIABLE; HttpStatus::STATUS_EXPECTATION_FAILED; HttpStatus::STATUS_IM_A_TEAPOT; HttpStatus::STATUS_MISDIRECTED_REQUEST; HttpStatus::STATUS_UNPROCESSABLE_ENTITY; HttpStatus::STATUS_LOCKED; HttpStatus::STATUS_FAILED_DEPENDENCY; HttpStatus::STATUS_TOO_EARLY; HttpStatus::STATUS_UPGRADE_REQUIRED; HttpStatus::STATUS_PRECONDITION_REQUIRED; HttpStatus::STATUS_TOO_MANY_REQUESTS; HttpStatus::STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE; HttpStatus::STATUS_UNAVAILABLE_FOR_LEGAL_REASONS; HttpStatus::STATUS_INTERNAL_SERVER_ERROR; HttpStatus::STATUS_NOT_IMPLEMENTED; HttpStatus::STATUS_BAD_GATEWAY; HttpStatus::STATUS_SERVICE_UNAVAILABLE; HttpStatus::STATUS_GATEWAY_TIMEOUT; HttpStatus::STATUS_VERSION_NOT_SUPPORTED; HttpStatus::STATUS_VARIANT_ALSO_NEGOTIATES; HttpStatus::STATUS_INSUFFICIENT_STORAGE; HttpStatus::STATUS_LOOP_DETECTED; HttpStatus::STATUS_NOT_EXTENDED; HttpStatus::STATUS_NETWORK_AUTHENTICATION_REQUIRED;
请查看/demo
目录以获取更多示例。