Introduction

Introduction – Introduction to URI_Template

Overview

URI Templates are strings that contain embedded variables that are transformed into URIs after embedded variables are substituted.

More information about URI Templates can be found at the following locations:

In a nutshell, URI Templates allow actors to specify a certain structure for URIs in order to make it possible for other actors to fill the structure with concrete information later. Joe Gregorio has posted an example of a possible use case.

Setup

Requirements

URI_Template requires PHP 5.

Installation

The URI_Template package can be installed using the PEAR installer command pear install URI_Template.

Alternative installation methods for situations where one has no access to the pear installer command can be found in the general installation chapter.

Uninstallation

Uninstalling the package can be done with pear uninstall URI_Template.

Example

<?php
require_once "URI/Template.php";

$values = array("a" => "foo""b" => "bar""data" => "10,20,30",
                
"points" => array(102030), "list0" => array(),
                
"str0" => """reserved" => ":/?#[]@!$&'()*+,;=",
                
"a_b" => "baz");

$t = new URI_Template("/{-append|/|a}{-opt|data|points}{-neg|@|a}{-prefix|#|b}");
echo 
$t->substitute($values); /* /foo/data#bar */

$t = new URI_Template("relative/{reserved}/");
echo 
$t->substitute($values); /* relative/%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D/ */

$t = new URI_Template("http://example.org/{foo=%25}/");
echo 
$t->substitute($values); /* http://example.org/%25/ */

$t = new URI_Template("http://example.org/?{-join|&|a,data}");
echo 
$t->substitute($values); /* http://example.org/?a=foo&data=10%2C20%2C30 */

$t = new URI_Template("http://example.org/?d={-listjoin|,|points}&{-join|&|a,b}");
echo 
$t->substitute($values); /* http://example.org/?d=10,20,30&a=foo&b=bar */

$t = new URI_Template("http://example.org/?d={-listjoin|,|list0}&{-join|&|foo}");
echo 
$t->substitute(array()); /* http://example.org/?d=& */

$t = new URI_Template("http://example.org/?d={-listjoin|&d=|points}");
echo 
$t->substitute($values); /* http://example.org/?d=10&d=20&d=30 */

$t = new URI_Template("http://example.org/{a}{b}/{a_b}");
echo 
$t->substitute($values); /* http://example.org/foobar/baz */

$t = new URI_Template("http://example.org/{a}{-prefix|/-/|a}/");
echo 
$t->substitute($values); /* http://example.org/foo/-/foo/ */
?>
URI_Template (Previous) Числа (Next)
Last updated: Sat, 25 Oct 2014 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report or add a note.
View this page in:

User Notes:

There are no user contributed notes for this page.