slim/http
Composer 安装命令:
composer require slim/http
包简介
Slim PSR-7 Object Decorators
README 文档
README
Slim PSR-7 Object Decorators
Installation
It's recommended that you use Composer to install this library.
$ composer require slim/http
This will install the slim/http component and all required dependencies.
PHP 7.4, or newer, is required.
Tests
To execute the test suite, you'll need to install all development dependencies.
$ git clone https://github.com/slimphp/Slim-Http
$ composer install
$ composer test
Usage
The Decoration Repo Provides 3 Factories which instantiate the Decorators. They respectively return PSR-7 Compatible Interfaces.
DecoratedResponseFactoryDecoratedServerRequestFactoryDecoratedUriFactory
Example for Instantiating a Decorated Nyholm/Psr7 Response
<?php use Nyholm\Psr7\Factory\Psr17Factory; use Slim\Http\Factory\DecoratedResponseFactory; $nyholmFactory = new Psr17Factory(); /** * DecoratedResponseFactory takes 2 parameters * @param \Psr\Http\Message\ResponseFactoryInterface which should be a ResponseFactory originating from the PSR-7 Implementation of your choice * @param \Psr\Http\Message\StreamFactoryInterface which should be a StreamFactory originating from the PSR-7 Implementation of your choice * Note: Nyholm/Psr17 has one factory which implements Both ResponseFactoryInterface and StreamFactoryInterface see https://github.com/Nyholm/psr7/blob/master/src/Factory/Psr17Factory.php */ $decoratedResponseFactory = new DecoratedResponseFactory($nyholmFactory, $nyholmFactory); /** * @var \Slim\Http\Response $response * The returned variable is a Response which has methods like withJson() */ $response = $decoratedResponseFactory->createResponse(200, 'OK'); $response = $response->withJson(['data' => [1, 2, 3]]);
Example for Instantiating a Decorated Laminas Diactoros Response
<?php use Laminas\Diactoros\ResponseFactory; use Laminas\Diactoros\StreamFactory; use Slim\Http\Factory\DecoratedResponseFactory; $responseFactory = new ResponseFactory(); $streamFactory = new StreamFactory(); /** * DecoratedResponseFactory takes 2 parameters * @param \Psr\Http\Message\ResponseFactoryInterface which should be a ResponseFactory originating from the PSR-7 Implementation of your choice * @param \Psr\Http\Message\StreamFactoryInterface which should be a StreamFactory originating from the PSR-7 Implementation of your choice */ $decoratedResponseFactory = new DecoratedResponseFactory($responseFactory, $streamFactory); /** * @var \Slim\Http\Response $response * The returned variable is a Response which has methods like withJson() */ $response = $decoratedResponseFactory->createResponse(200, 'OK'); $response = $response->withJson(['data' => [1, 2, 3]]);
Decorated Response Object Methods
The decorated ResponseInterface provides the following additional methods:
Response::withJson($data, $status, $options, $depth)
| Parameter | Type | Description |
|---|---|---|
| $data | mixed |
The data to encode |
| $status | int |
The HTTP Status Code |
| $depth | int |
JSON encoding max depth |
Response::withFileDownload($file, $name)
Triggers the client to download the specified file.
| Parameter | Type | Description |
|---|---|---|
| $file | `string | resource |
| $name | `string | null` |
| $contentType | `bool | string` |
Response::withFile($file, $contentType)
Response with file to client
| Parameter | Type | Description |
|---|---|---|
| $file | `string | resource |
| $contentType | `bool | string` |
Response::withRedirect($url, $status)
| Parameter | Type | Description |
|---|---|---|
| $url | string |
The redirect destination url |
| $status | int |
The HTTP Status Code |
Response::write($data)
| Parameter | Type | Description |
|---|---|---|
| $url | string |
The data to write to the Response body |
Response::isClientError()
Assert the underlying response's status code is between 400 and 500.
Response::isEmpty()
Assert the underlying response's status code is 204, 205 or 304.
Response::isForbidden()
Assert the underlying response's status code is 403.
Response::isInformational()
Assert the underlying response's status code is between 100 and 200.
Response::isOk()
Assert the underlying response's status code is 200.
Response::isNotFound()
Assert the underlying response's status code is 404.
Response::isRedirect()
Assert the underlying response's status code is 301, 302, 303, 307 or 308.
Response::isRedirection()
Assert the underlying response's status code is between 300 and 400.
Response::isServerError()
Assert the underlying response's status code is between 500 and 600.
Response::isSuccessful()
Assert the underlying response's status code is between 200 and 300.
Response::__toString()
Will return a string formatted representation of the underlying response object.
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
{"Hello": "World"}
Decorated ServerRequest Object Methods
The decorated ServerRequestInterface provides the following additional methods:
ServerRequest::withAttributes($attributes)
| Parameter | Type | Description |
|---|---|---|
| $attributes | array |
Attributes to be appended to the request |
ServerRequest::getContentCharset()
Returns the detected charset from the Content-Type header of the underlying server request object. Returns null if no value is present.
ServerRequest::getContentType()
Returns the value from the Content-Type header of the underlying server request object. Returns null if no value is present.
ServerRequest::getContentLength()
Returns the value from the Content-Length header of the underlying server request object. Returns null if no value is present.
ServerRequest::getCookieParam($key, $default)
| Parameter | Type | Description |
|---|---|---|
| $key | string |
The attribute name |
| $default | mixed |
Default value to return if the attribute does not exist |
ServerRequest::getMediaType()
Returns the first detected value from the Content-Type header of the underlying server request object. Returns null if no value is present.
ServerRequest::getMediaTypeParams()
Returns an array of detected values from the Content-Type header of the underlying server request object. Returns an empty array if no values are present.
ServerRequest::getParam($key, $default)
Returns the value from key in $_POST or $_GET
| Parameter | Type | Description |
|---|---|---|
| $key | string |
The attribute name |
| $default | mixed |
Default value to return if the attribute does not exist |
ServerRequest::getParams()
Returns a merged associative array of the $_POST and $_GET parameters.
ServerRequest::getParsedBody()
Returns the parsed body from the underlying server request object if it already has been parsed by the underlying PSR-7 implementation. If the parsed body is empty, our decorator attempts to detect the content type and parse the body using one of the registered media type parsers.
The default media type parsers support:
- JSON
- XML
You can register your own media type parser using the ServerRequest::registerMediaTypeParser() method.
ServerRequest::getParsedBodyParam($key, $default)
Returns the value from key in the parsed body of the underlying server request object.
| Parameter | Type | Description |
|---|---|---|
| $key | string |
The attribute name |
| $default | mixed |
Default value to return if the attribute does not exist |
ServerRequest::getQueryParam($key, $default)
Returns the value from key in the parsed ServerRequest query string
| Parameter | Type | Description |
|---|---|---|
| $key | string |
The attribute name |
| $default | mixed |
Default value to return if the attribute does not exist |
ServerRequest::getServerParam($key, $default)
Returns the value from key in parsed server parameters from the underlying underlying server request object.
| Parameter | Type | Description |
|---|---|---|
| $key | string |
The attribute name |
| $default | mixed |
Default value to return if the attribute does not exist |
ServerRequest::registerMediaTypeParser($key, $default)
Returns the value from key in parsed server parameters from the underlying server request object.
| Parameter | Type | Description |
|---|---|---|
| $mediaType | string |
A HTTP media type (excluding content-type params) |
| $callable | callable |
A callable that returns parsed contents for media type |
ServerRequest::isMethod($method)
| Parameter | Type | Description |
|---|---|---|
| $method | string |
The method name |
ServerRequest::isDelete()
Asserts that the underlying server request's method is DELETE
ServerRequest::isGet()
Asserts that the underlying server request's method is GET
ServerRequest::isHead()
Asserts that the underlying server request's method is HEAD
ServerRequest::isOptions()
Asserts that the underlying server request's method is OPTIONS
ServerRequest::isPatch()
Asserts that the underlying server request's method is PATCH
ServerRequest::isPost()
Asserts that the underlying server request's method is POST
ServerRequest::isPut()
Asserts that the underlying server request's method is PUT
ServerRequest::isXhr()
Asserts that the header X-Requested-With from the underlying server request is XMLHttpRequest
Decorated Uri Object Methods
The decorated UriInterface provides the following additional methods:
Uri::getBaseUrl()
Returns the fully qualified base URL of the underlying uri object.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover security related issues, please email security@slimframework.com instead of using the issue tracker.
Credits
License
This component is licensed under the MIT license. See License File for more information.
slim/http 适用场景与选型建议
slim/http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.45M 次下载、GitHub Stars 达 150, 最近一次更新时间为 2015 年 12 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「psr-7」 「psr7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 slim/http 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 slim/http 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 slim/http 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Model of a web-based resource
Interfaces for web resource models and services to retrieve and create them
Skolkovo API Client
Interface for a factory to create Psr\Http\Message\StreamInterface objects
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 2.45M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 153
- 点击次数: 26
- 依赖项目数: 77
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2015-12-23