PHPackages                             werx/url - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. werx/url

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

werx/url
========

Build urls to resources in your app

1.2.0(11y ago)15.5k1MITPHPPHP &gt;= 5.4

Since Jul 22Pushed 11y ago1 watchersCompare

[ Source](https://github.com/werx/url)[ Packagist](https://packagist.org/packages/werx/url)[ RSS](/packages/werx-url/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (4)Versions (6)Used By (1)

werx\\Url
=========

[](#werxurl)

[![Build Status](https://camo.githubusercontent.com/2638e98cb7b645418fdd9c7399a598df5d15f3e124d47f8a3a7d3e8fa5ca7b00/68747470733a2f2f7472617669732d63692e6f72672f776572782f75726c2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/werx/url) [![Total Downloads](https://camo.githubusercontent.com/082dc5cd63e73827b81669cc58aac47761ac5a70fa596e463a87920eb616b291/68747470733a2f2f706f7365722e707567782e6f72672f776572782f75726c2f646f776e6c6f6164732e706e67)](https://packagist.org/packages/werx/url) [![Latest Stable Version](https://camo.githubusercontent.com/19acdebac0cc1bfcaee8558a3104f98b6a26fec60abcf6c24af4b34aff707a14/68747470733a2f2f706f7365722e707567782e6f72672f776572782f75726c2f762f737461626c652e706e67)](https://packagist.org/packages/werx/url)

Build urls to resources in your app. Available as a standalone library or as a [Plates Extension](http://platesphp.com/extensions/).

This library uses [rize\\UriTemplate](https://github.com/rize/UriTemplate) for the heavy lifting of expanding urls with additional functionality to make it easier to build urls to resources within your application.

Usage
-----

[](#usage)

### Creating an instance of the url builder.

[](#creating-an-instance-of-the-url-builder)

Get an instance of the url builder. If you pass no constructor values, it will base all urls on the root-relative path to the currently executing script using `$_SERVER['SCRIPT_NAME']`.

```
$builder = new \werx\Url\Builder;
```

You can also provide a base url and script name.

```
$builder = new \werx\Url\Builder('/path/to/app/', 'index.php');
```

If you set a fully qualified url in the constructor, it will be used when building urls.

```
$builder = new \werx\Url\Builder('http://example.com/path/to/app/', 'index.php');
```

### Building URLs.

[](#building-urls)

Simple pattern replacement with a single `id`

```
$url = $builder->action('home/details/{id}', 5);
var_dump($url); # /path/to/app/index.php/home/details/5
```

Multiple pattern replacements

```
$url = $builder->action('home/{action}/{id}', ['action' => 'details', 'id' => 5]);
var_dump($url); # /path/to/app/index.php/home/details/5
```

Convert an array to a query string.

```
$url = $builder->query('home/search', ['name' => 'Josh', 'state' => 'AR']);
var_dump($url); # /path/to/app/index.php/home/search?name=Josh&state=AR
```

Build an url to a static resource in your application.

```
$url = $builder->asset('images/logo.png');
var_dump($url); # /path/to/app/images/logo.png
```

Plates Extension
----------------

[](#plates-extension)

This library includes an extension that allows it to be used within [Plates Templates](http://platesphp.com/).

In your controller...

```
$engine = new \League\Plates\Engine('/path/to/templates');
$ext = new \werx\Url\Extensions\Plates;
$engine->loadExtension($ext);

$template = new \League\Plates\Template($engine);
$output = $template->render('home');
```

Then in your view, you can call any of the `werx\Url\Builder` methods via `$this->url()->methodtocall()`.

```
