Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Uri Template Laravel Package

guzzlehttp/uri-template

RFC 6570 URI Template expansion for PHP. Build URLs by expanding templates with variables, supporting reserved, fragment, label, path, query, and form-style operators. Lightweight component from the Guzzle ecosystem.

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package with composer require guzzlehttp/uri-template. Start by using the simple static API to expand URIs using RFC 6570 templates:

use GuzzleHttp\UriTemplate\UriTemplate;

$template = 'https://api.example.com/users{/id}{?filter*}';
$params = ['id' => 123, 'filter' => ['active' => true, 'role' => 'admin']];

echo UriTemplate::expand($template, $params); 
// Outputs: https://api.example.com/users/123?active=true&role=admin

Check the README for basic usage and the RFC 6570 spec for template syntax (e.g., {/var}, {?key*}).

Implementation Patterns

  • Static API: Use UriTemplate::expand() for one-off expansions.
  • Class-based expansion: Instantiate UriTemplate for repeated use (e.g., in service classes or SDKs):
    $uriTemplate = new UriTemplate();
    $url = $uriTemplate->expand('/users{/id}', ['id' => 42]);
    
  • Integration with Guzzle: Often used internally by Guzzle, but standalone usage is common in HTTP client libraries and API SDKs to support templated endpoints.
  • Array expansion with * suffix: Leverage {'key*'} syntax to safely expand arrays and objects (e.g., {'query*'} => ['page' => 1, 'size' => 20]).
  • Custom encoding: No custom encoding—relies on PHP’s built-in rawurlencode() for RFC 3986-compliant encoding.

Gotchas and Tips

  • PHP 8.5+ only: While backwards compatible with earlier versions (v0.2+ supports PHP 7.1+), recent releases (v1.0+) drop older PHP support—ensure your runtime matches.
  • 0 values expand correctly: Prior to v1.0.2, 0 used in arrays/objects would be skipped; now it expands as "0". Double-check if upgrading from older versions.
  • No built-in validation: Templates are expanded per RFC 6570 spec, but invalid templates or mismatched variables may produce unexpected or malformed URIs—test edge cases.
  • Empty arrays/objects: Empty arrays expand to nothing (?{list*} with list=[] yields ""). Use default values (e.g., ['list' => null]) if omission is desired.
  • Path vs query confusion: Use / vs ? prefix in templates carefully—{/var} affects the path; {?var} affects the query string.
  • Extensibility: No hooks or plugins; the class is final. Extend indirectly via composition (e.g., a UriExpander service wrapper).
  • Debug tip: Use var_dump(UriTemplate::expand(...)) with known inputs to validate expansions incrementally, especially for nested objects.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport