Converts the data in the record set to a different format, i.e. an array. Available
options: array, URL, stream context configuration, or string.
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. |
Результат | mixed | Varies; see the `$format` parameter for possible return values. |
/** * 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); }
public function testToContextWithAuth() { $request = new Request(array('auth' => 'Basic', 'username' => 'Aladdin', 'password' => 'open sesame')); $expected = array('http' => array('method' => 'GET', 'header' => array('Host: localhost', 'Connection: Close', 'User-Agent: Mozilla/5.0', 'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='), 'content' => '', 'protocol_version' => '1.1', 'ignore_errors' => true)); $this->assertEqual($expected, $request->to('context')); }
public function testKeepDefinedContentTypeHeaderWhenTypeIsSet() { $request = new Request(array('method' => 'POST', 'type' => 'json', 'headers' => array('Content-Type' => 'text/x-test'))); $expected = 'Content-Type: text/x-test'; $result = $request->headers(); $message = "Expected value `{$expected}` not found in result."; $this->assertTrue(in_array($expected, $result), $message); $expected = '#Content-Type: text/x-test#'; $result = $request->to('string'); $this->assertPattern($expected, $result); }
/** * 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('path' => $this->env('base') . '/' . $this->url); return parent::to($format, $options + $defaults); }
public function testParseUrlToConfig() { $url = "http://localhost/path/one.php?param=1¶m=2"; $config = parse_url($url); $request = new Request($config); $expected = $url; $result = $request->to('url'); $this->assertEqual($expected, $result); }