Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A set of PSR-7 object decorators providing useful convenience methods

License

NotificationsYou must be signed in to change notification settings

slimphp/Slim-Http

Slim PSR-7 Object Decorators

Build StatusCoverage StatusTotal DownloadsLicense

Installation

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.

Tests

To execute the test suite, you'll need to install all development dependencies.

$ git clone https://github.com/slimphp/Slim-Http$ composer install$ composertest

Usage

The Decoration Repo Provides 3 Factories which instantiate the Decorators. They respectively return PSR-7 Compatible Interfaces.

  • DecoratedResponseFactory
  • DecoratedServerRequestFactory
  • DecoratedUriFactory

Example for Instantiating a Decorated Nyholm/Psr7 Response

<?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]]);

Example for Instantiating a Decorated Laminas Diactoros Response

<?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]]);

Decorated Response Object Methods

The decoratedResponseInterface provides the following additional methods:

Response::withJson($data, $status, $options, $depth)

ParameterTypeDescription
$datamixedThe data to encode
$statusintThe HTTP Status Code
$depthintJSON encoding max depth

Response::withFileDownload($file, $name)

Triggers the client to download the specified file.

ParameterTypeDescription
$file`stringresource
$name`stringnull`
$contentType`boolstring`

Response::withFile($file, $contentType)

Response with file to client

ParameterTypeDescription
$file`stringresource
$contentType`boolstring`

Response::withRedirect($url, $status)

ParameterTypeDescription
$urlstringThe redirect destination url
$statusintThe HTTP Status Code

Response::write($data)

ParameterTypeDescription
$urlstringThe data to write to theResponse body

Response::isClientError()

Assert the underlying response's status code is between400 and500.

Response::isEmpty()

Assert the underlying response's status code is204, 205 or304.

Response::isForbidden()

Assert the underlying response's status code is403.

Response::isInformational()

Assert the underlying response's status code is between100 and200.

Response::isOk()

Assert the underlying response's status code is200.

Response::isNotFound()

Assert the underlying response's status code is404.

Response::isRedirect()

Assert the underlying response's status code is301,302,303,307 or308.

Response::isRedirection()

Assert the underlying response's status code is between300 and400.

Response::isServerError()

Assert the underlying response's status code is between500 and600.

Response::isSuccessful()

Assert the underlying response's status code is between200 and300.

Response::__toString()

Will return a string formatted representation of the underlying response object.

HTTP/1.1 200 OKContent-Type: application/json;charset=utf-8{"Hello": "World"}

Decorated ServerRequest Object Methods

The decoratedServerRequestInterface provides the following additional methods:

ServerRequest::withAttributes($attributes)

ParameterTypeDescription
$attributesarrayAttributes to be appended to the request

ServerRequest::getContentCharset()

Returns the detected charset from theContent-Type header of the underlying server request object. Returnsnull if no value is present.

ServerRequest::getContentType()

Returns the value from theContent-Type header of the underlying server request object. Returnsnull if no value is present.

ServerRequest::getContentLength()

Returns the value from theContent-Length header of the underlying server request object. Returnsnull if no value is present.

ServerRequest::getCookieParam($key, $default)

ParameterTypeDescription
$keystringThe attribute name
$defaultmixedDefault value to return if the attribute does not exist

ServerRequest::getMediaType()

Returns the first detected value from theContent-Type header of the underlying server request object. Returnsnull if no value is present.

ServerRequest::getMediaTypeParams()

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.

ServerRequest::getParam($key, $default)

Returns the value from key in$_POST or$_GET

ParameterTypeDescription
$keystringThe attribute name
$defaultmixedDefault 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 theServerRequest::registerMediaTypeParser() method.

ServerRequest::getParsedBodyParam($key, $default)

Returns the value from key in the parsed body of the underlying server request object.

ParameterTypeDescription
$keystringThe attribute name
$defaultmixedDefault value to return if the attribute does not exist

ServerRequest::getQueryParam($key, $default)

Returns the value from key in the parsedServerRequest query string

ParameterTypeDescription
$keystringThe attribute name
$defaultmixedDefault 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.

ParameterTypeDescription
$keystringThe attribute name
$defaultmixedDefault 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.

ParameterTypeDescription
$mediaTypestringA HTTP media type (excluding content-type params)
$callablecallableA callable that returns parsed contents for media type

ServerRequest::isMethod($method)

ParameterTypeDescription
$methodstringThe method name

ServerRequest::isDelete()

Asserts that the underlying server request's method isDELETE

ServerRequest::isGet()

Asserts that the underlying server request's method isGET

ServerRequest::isHead()

Asserts that the underlying server request's method isHEAD

ServerRequest::isOptions()

Asserts that the underlying server request's method isOPTIONS

ServerRequest::isPatch()

Asserts that the underlying server request's method isPATCH

ServerRequest::isPost()

Asserts that the underlying server request's method isPOST

ServerRequest::isPut()

Asserts that the underlying server request's method isPUT

ServerRequest::isXhr()

Asserts that the headerX-Requested-With from the underlying server request isXMLHttpRequest

Decorated Uri Object Methods

The decoratedUriInterface provides the following additional methods:

Uri::getBaseUrl()

Returns the fully qualified base URL of the underlying uri object.

Contributing

Please seeCONTRIBUTING for details.

Security

If you discover security related issues, please emailsecurity@slimframework.cominstead of using the issue tracker.

Credits

License

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

Stars

Watchers

Forks

Packages

No packages published

Contributors25

Languages


[8]ページ先頭

©2009-2025 Movatter.jp