Last active
January 29, 2020 12:11
-
-
Save nyamsprod/7eeee674eb64e8384637e9c07239d3b7 to your computer and use it in GitHub Desktop.
Usage examples of the league uri template class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use League\Uri\UriTemplate; | |
//$template is a string which complies to RFC6570 | |
$template = 'https://example.com/hotels/{hotel}/bookings/{booking}'; | |
$uriTemplate = new UriTemplate($template); | |
//$variables are the parameters that will be replaced in the template | |
$variables = ['booking' => '42', 'hotel' => 'Rest & Relax']; | |
// the returned $uri is an League\Uri\Uri instance. | |
$uri = $uriTemplate->expand($variables); | |
echo $uri, PHP_EOL; | |
// https://example.com/hotels/Rest%20%26%20Relax/bookings/42 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use League\Uri\UriTemplate; | |
$template = 'https://api.twitter.com/{version}/search/{term:1}/{term}/{?q*,limit}'; | |
$defaultVariables = ['version' => '1.1']; | |
$uriTemplate = new UriTemplate($template, $defaultVariables); | |
$params = [ | |
'term' => 'john', | |
'q' => ['a', 'b'], | |
'limit' => '10', | |
'version' => '2.0', | |
]; | |
echo $uriTemplate->expand($params), PHP_EOL; | |
// https://api.twitter.com/2.0/search/j/john/?q=a&q=b&limit=10 | |
~~~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use League\Uri\UriTemplate; | |
$template = 'https://api.twitter.com/{version}/search/{term:1}/{term}/{?q*,limit}'; | |
$defaultVariables = ['version' => '1.1']; | |
$uriTemplate = new UriTemplate($template, $defaultVariables); | |
$params = [ | |
'term' => 'john', | |
'q' => ['a', 'b'], | |
'limit' => '10', | |
]; | |
echo $uriTemplate->expand($params), PHP_EOL; | |
// https://api.twitter.com/1.1/search/j/john/?q=a&q=b&limit=10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment