public to ( string $format, array $options = [] ) : mixed | ||
$format | string | Format to convert to. Should be either `'url'`, which returns a string representation of the URL that this request points to, or `'context'`, which returns an array usable with PHP's `stream_context_create()` function. For more available formats, see the parent method, `lithium\net\Message::to()`. |
$options | array | Allows overriding of specific portions of the URL, as follows. These options should only be specified if you intend to replace the values that are already in the `Request` object. - `'scheme'` _string_: The protocol scheme of the URL. - `'method'` _string_: If applicable, the HTTP method to use in the request. Mainly applies to the `'context'` format. - `'host'` _string_: The host name the request is pointing at. - `'port'` _string_: The host port, if any. - `'path'` _string_: The URL path. - `'query'` _mixed_: The query string of the URL as a string or array. - `'auth'` _string_: Authentication information. See the constructor for details. - `'content'` _string_: The body of the request. - `'headers'` _array_: The request headers. - `'version'` _string_: The HTTP version of the request, where applicable. |
return | mixed | Varies; see the `$format` parameter for possible return values. |
public function to($format, array $options = array())
{
$defaults = array('method' => $this->method, 'scheme' => $this->scheme, 'host' => $this->host, 'port' => $this->port, 'path' => $this->path, 'query' => null, 'auth' => $this->auth, 'username' => $this->username, 'password' => $this->password, 'headers' => array(), 'cookies' => array(), 'proxy' => $this->_config['proxy'], 'body' => null, 'version' => $this->version, 'ignore_errors' => $this->_config['ignoreErrors'], 'follow_location' => $this->_config['followLocation'], 'request_fulluri' => (bool) $this->_config['proxy']);
$options += $defaults;
if (is_string($options['query'])) {
$options['query'] = "?" . $options['query'];
} elseif ($options['query']) {
$options['query'] = "?" . http_build_query($options['query']);
} elseif ($options['query'] === null) {
$options['query'] = $this->queryString();
}
if ($options['auth']) {
$data = array();
if (is_array($options['auth']) && !empty($options['auth']['nonce'])) {
$data = array('method' => $options['method'], 'uri' => $options['path']);
$data += $options['auth'];
}
$auth = $this->_classes['auth'];
$data = $auth::encode($options['username'], $options['password'], $data);
$this->headers('Authorization', $auth::header($data));
}
if ($this->cookies($options['cookies'])) {
$this->headers('Cookie', $this->_cookies());
}
$body = $this->body($options['body']);
if ($body || !in_array($options['method'], array('GET', 'HEAD', 'DELETE'))) {
$this->headers('Content-Length', strlen($body));
}
$conv = isset($this->_formats[$format]) ? $this->_formats[$format] : null;
return $conv ? $conv($this, $options, $defaults) : parent::to($format, $options);
}
/** * Overrides `lithium\net\http\Request::to()` to provide the correct options for generating * URLs. For information about this method, see the parent implementation. * * @see lithium\net\http\Request::to() * @param string $format The format to convert to. * @param array $options Override options. * @return mixed The return value type depends on `$format`. */ public function to($format, array $options = array()) { $defaults = array('scheme' => $this->env('HTTPS') ? 'https' : 'http', 'host' => $this->env('HTTP_HOST'), 'path' => $this->_base . $this->url, 'query' => $this->query); return parent::to($format, $options + $defaults); }