$method
$method : string
HTTP Request encapsulation
Requests are considered immutable; all methods that might change state are implemented such that they retain the internal state of the current message and return a new instance that contains the changed state.
$uri : \Psr\Http\Message\UriInterface
getRequestTarget() : string
Retrieves the message's request target.
Retrieves the message's request-target either as it will appear (for clients), as it appeared at request (for servers), or as it was specified for the instance (see withRequestTarget()).
In most cases, this will be the origin-form of the composed URI, unless a value was provided to the concrete implementation (see withRequestTarget() below).
If no URI is available, and no request-target has been specifically provided, this method MUST return the string "/".
withRequestTarget(mixed $requestTarget) : static
Create a new instance with a specific request-target.
If the request needs a non-origin-form request-target — e.g., for specifying an absolute-form, authority-form, or asterisk-form — this method may be used to create an instance with the specified request-target, verbatim.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return a new instance that has the changed request target.
mixed | $requestTarget |
if the request target is invalid.
withMethod(string $method) : static
Return an instance with the provided HTTP method.
While HTTP method names are typically all uppercase characters, HTTP method names are case-sensitive and thus implementations SHOULD NOT modify the given string.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the changed request method.
string | $method | Case-insensitive method. |
for invalid HTTP methods.
getUri() : \Psr\Http\Message\UriInterface
Retrieves the URI instance.
This method MUST return a UriInterface instance.
Returns a UriInterface instance representing the URI of the request, if any.
withUri(\Psr\Http\Message\UriInterface $uri, boolean $preserveHost = false) : static
Returns an instance with the provided URI.
This method will update the Host header of the returned request by default if the URI contains a host component. If the URI does not contain a host component, any pre-existing Host header will be carried over to the returned request.
You can opt-in to preserving the original state of the Host header by
setting $preserveHost
to true
. When $preserveHost
is set to
true
, the returned request will not update the Host header of the
returned message -- even if the message contains no Host header. This
means that a call to getHeader('Host')
on the original request MUST
equal the return value of a call to getHeader('Host')
on the returned
request.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the new UriInterface instance.
\Psr\Http\Message\UriInterface | $uri | New request URI to use. |
boolean | $preserveHost | Preserve the original state of the Host header. |
__construct(null|string|\Psr\Http\Message\UriInterface $uri = null, null|string $method = null, string|resource|\Psr\Http\Message\StreamInterface $body = 'php://temp', array $headers = array())
null|string|\Psr\Http\Message\UriInterface | $uri | URI for the request, if any. |
null|string | $method | HTTP method for the request, if any. |
string|resource|\Psr\Http\Message\StreamInterface | $body | Message body, if any. |
array | $headers | Headers for the message, if any. |
for any invalid value.
initialize(null|string|\Psr\Http\Message\UriInterface $uri = null, null|string $method = null, string|resource|\Psr\Http\Message\StreamInterface $body = 'php://memory', array $headers = array())
Initialize request state.
Used by constructors.
null|string|\Psr\Http\Message\UriInterface | $uri | URI for the request, if any. |
null|string | $method | HTTP method for the request, if any. |
string|resource|\Psr\Http\Message\StreamInterface | $body | Message body, if any. |
array | $headers | Headers for the message, if any. |
for any invalid value.
createUri(null|string|\Psr\Http\Message\UriInterface $uri) : \Psr\Http\Message\UriInterface
Create and return a URI instance.
If $uri
is a already a UriInterface
instance, returns it.
If $uri
is a string, passes it to the Uri
constructor to return an
instance.
If $uri is null, creates and returns an empty
Uri` instance.
Otherwise, it raises an exception.
null|string|\Psr\Http\Message\UriInterface | $uri |
<?php
/**
* @see https://github.com/zendframework/zend-diactoros for the canonical source repository
* @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Diactoros;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use function strtolower;
/**
* HTTP Request encapsulation
*
* Requests are considered immutable; all methods that might change state are
* implemented such that they retain the internal state of the current
* message and return a new instance that contains the changed state.
*/
class Request implements RequestInterface
{
use RequestTrait;
/**
* @param null|string|UriInterface $uri URI for the request, if any.
* @param null|string $method HTTP method for the request, if any.
* @param string|resource|StreamInterface $body Message body, if any.
* @param array $headers Headers for the message, if any.
* @throws \InvalidArgumentException for any invalid value.
*/
public function __construct($uri = null, $method = null, $body = 'php://temp', array $headers = [])
{
$this->initialize($uri, $method, $body, $headers);
}
/**
* {@inheritdoc}
*/
public function getHeaders()
{
$headers = $this->headers;
if (! $this->hasHeader('host')
&& $this->uri->getHost()
) {
$headers['Host'] = [$this->getHostFromUri()];
}
return $headers;
}
/**
* {@inheritdoc}
*/
public function getHeader($header)
{
if (! $this->hasHeader($header)) {
if (strtolower($header) === 'host'
&& $this->uri->getHost()
) {
return [$this->getHostFromUri()];
}
return [];
}
$header = $this->headerNames[strtolower($header)];
return $this->headers[$header];
}
}