Skip to content

Commit 511a71c

Browse files
committed
Improve Uri documentation
1 parent 26c62fe commit 511a71c

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

docs/uri/7.0/uri-template.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ echo $uri; //display https://example.com/hotels/Rest%20%26%20Relax/bookings/42"
3333
<p class="message-notice"><code>expandToUri()</code> and <code>expandToUrl()</code> are available since
3434
version <code>7.6.0</code></p>
3535

36+
<p class="message-info">Since version <code>7.6</code> the method takes a second optional parameter
37+
which serves as a base URI. The generated URI is resolved against the Base URI.</p>
38+
39+
~~~php
40+
<?php
41+
42+
use League\Uri\UriTemplate;
43+
44+
$template = '/hotels/{hotel}/bookings/{booking}';
45+
$params = ['booking' => '42', 'hotel' => 'Rest & Relax'];
46+
47+
$uriTemplate = new UriTemplate($template);
48+
$uri = $uriTemplate->expand($params, 'https://example.com'); // instance of League\Uri\Uri
49+
$rfc3986Uri = $uriTemplate->expandToUri($params, 'https://example.com'); // instance of Uri\Rfc3986\Uri
50+
$whatWgUrl = $uriTemplate->expandToUrl($params, 'https://example.com'); // instance of Uri\Whatwg\Url
51+
52+
echo $uri; //display https://example.com/hotels/Rest%20%26%20Relax/bookings/42"
53+
~~~
54+
3655
## Template variables
3756

3857
<p class="message-notice">For maximum interoperability you should make sure your variables are
@@ -78,11 +97,11 @@ echo $uriTemplate->expand($params), PHP_EOL;
7897
//displays https://api.twitter.com/2.0/search/j/john/?q=a&q=b&limit=10
7998
~~~
8099

81-
The `expandToUri()` and the `expandToUrl()` methods will act exactly like the `expand()` method
100+
The `expandToUri()` and the `expandToUrl()` methods will act exactly like the `expand()` method
82101
but will instead return a `Uri\Rfc3986\Uri` and a `Uri\Whatwg\Url` object respectively.
83102

84103
<p class="message-warning">a WHATWG URL must always be absolute if the URI template is not you
85-
should provide to <code>expandToUrl()</code> a base URL otherwise an <code>Uri\WhatWg\InvalidUrlException</code>
104+
MUST provide a base URL to <code>expandToUrl()</code> otherwise an <code>Uri\WhatWg\InvalidUrlException</code>
86105
exception will be thrown.</p>
87106

88107
~~~php
@@ -102,10 +121,6 @@ echo $uriTemplate->expandToUrl($params, 'https://api.twitter.com')->toAsciiStrin
102121
echo $uriTemplate->expandToUrl($params); //will throw
103122
~~~
104123

105-
<p class="message-info">The same feature of adding a base URI is also supported for the
106-
other two methods <code>expand()</code> and <code>expandToUri</code> but only if the
107-
baseURI variable is not <code>null</code>.</p>
108-
109124
### Updating the default variables
110125

111126
At any given time you may update your default variables but since the `UriTemplate`

uri/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ All Notable changes to `League\Uri` will be documented in this file
3535
- `UriTemplate` implements the `Stringable` interface
3636
- `UriTemplate::expandToUri` and `UriTemplate::expandToUrl`
3737
- `UriTemplate::expandToUriOrFail` and `UriTemplate::expandToUrlOrFail`
38+
- `UriTemplate::expand` takes a second optional parameter which represents a base URI to resolve the URI from.
3839
- Dependency to `psr/http-factory` package which is required for the package.
3940
- Support for `Uri\Rfc3986\Uri` and `Uri\Whatwg\Url`
4041

0 commit comments

Comments
 (0)