- Notifications
You must be signed in to change notification settings - Fork46
A set of PSR-7 object decorators providing useful convenience methods
License
slimphp/Slim-Http
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Slim PSR-7 Object Decorators
It's recommended that you useComposer to installthis library.
$ composer require slim/http
This will install theslim/http component and all required dependencies.PHP 7.4, or newer, is required.
To execute the test suite, you'll need to install all development dependencies.
$ git clone https://github.com/slimphp/Slim-Http$ composer install$ composertestThe Decoration Repo Provides 3 Factories which instantiate the Decorators. They respectively return PSR-7 Compatible Interfaces.
DecoratedResponseFactoryDecoratedServerRequestFactoryDecoratedUriFactory
<?phpuseNyholm\Psr7\Factory\Psr17Factory;useSlim\Http\Factory\DecoratedResponseFactory;$nyholmFactory =newPsr17Factory();/** * 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 =newDecoratedResponseFactory($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]]);
<?phpuseLaminas\Diactoros\ResponseFactory;useLaminas\Diactoros\StreamFactory;useSlim\Http\Factory\DecoratedResponseFactory;$responseFactory =newResponseFactory();$streamFactory =newStreamFactory();/** * 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 =newDecoratedResponseFactory($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]]);
The decoratedResponseInterface provides the following additional methods:
| Parameter | Type | Description |
|---|---|---|
| $data | mixed | The data to encode |
| $status | int | The HTTP Status Code |
| $depth | int | JSON encoding max depth |
Triggers the client to download the specified file.
| Parameter | Type | Description |
|---|---|---|
| $file | `string | resource |
| $name | `string | null` |
| $contentType | `bool | string` |
Response with file to client
| Parameter | Type | Description |
|---|---|---|
| $file | `string | resource |
| $contentType | `bool | string` |
| Parameter | Type | Description |
|---|---|---|
| $url | string | The redirect destination url |
| $status | int | The HTTP Status Code |
| Parameter | Type | Description |
|---|---|---|
| $url | string | The data to write to theResponse body |
Assert the underlying response's status code is between400 and500.
Assert the underlying response's status code is204, 205 or304.
Assert the underlying response's status code is403.
Assert the underlying response's status code is between100 and200.
Assert the underlying response's status code is200.
Assert the underlying response's status code is404.
Assert the underlying response's status code is301,302,303,307 or308.
Assert the underlying response's status code is between300 and400.
Assert the underlying response's status code is between500 and600.
Assert the underlying response's status code is between200 and300.
Will return a string formatted representation of the underlying response object.
HTTP/1.1 200 OKContent-Type: application/json;charset=utf-8{"Hello": "World"}The decoratedServerRequestInterface provides the following additional methods:
| Parameter | Type | Description |
|---|---|---|
| $attributes | array | Attributes to be appended to the request |
Returns the detected charset from theContent-Type header of the underlying server request object. Returnsnull if no value is present.
Returns the value from theContent-Type header of the underlying server request object. Returnsnull if no value is present.
Returns the value from theContent-Length header of the underlying server request object. Returnsnull if no value is present.
| Parameter | Type | Description |
|---|---|---|
| $key | string | The attribute name |
| $default | mixed | Default value to return if the attribute does not exist |
Returns the first detected value from theContent-Type header of the underlying server request object. Returnsnull if no value is present.
Returns an array of detected values from theContent-Type header of the underlying server request object. Returns an empty array if no values are present.
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 |
Returns a merged associative array of the$_POST and$_GET parameters.
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 theServerRequest::registerMediaTypeParser() method.
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 |
Returns the value from key in the parsedServerRequest query string
| Parameter | Type | Description |
|---|---|---|
| $key | string | The attribute name |
| $default | mixed | Default value to return if the attribute does not exist |
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 |
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 |
| Parameter | Type | Description |
|---|---|---|
| $method | string | The method name |
Asserts that the underlying server request's method isDELETE
Asserts that the underlying server request's method isGET
Asserts that the underlying server request's method isHEAD
Asserts that the underlying server request's method isOPTIONS
Asserts that the underlying server request's method isPATCH
Asserts that the underlying server request's method isPOST
Asserts that the underlying server request's method isPUT
Asserts that the headerX-Requested-With from the underlying server request isXMLHttpRequest
The decoratedUriInterface provides the following additional methods:
Returns the fully qualified base URL of the underlying uri object.
Please seeCONTRIBUTING for details.
If you discover security related issues, please emailsecurity@slimframework.cominstead of using the issue tracker.
This component is licensed under the MIT license. SeeLicense Filefor more information.
About
A set of PSR-7 object decorators providing useful convenience methods
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.