PHPackages                             gajus/director - 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. gajus/director

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

gajus/director
==============

Utility for generating URLs relative to the predefined routes and for handling the redirects.

1.0.1(11y ago)141[1 issues](https://github.com/gajus/director/issues)BSD-3-ClausePHPPHP &gt;=5.4

Since Apr 16Pushed 9y ago2 watchersCompare

[ Source](https://github.com/gajus/director)[ Packagist](https://packagist.org/packages/gajus/director)[ Docs](https://github.com/gajus/director)[ RSS](/packages/gajus-director/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (2)Versions (4)Used By (0)

Director
========

[](#director)

[![Build Status](https://camo.githubusercontent.com/59f6d9c068b34ba632b65d77a929ee0540ab4039abf316d2aef92523df6ee7ab/68747470733a2f2f7472617669732d63692e6f72672f67616a75732f6469726563746f722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/gajus/director)[![Coverage Status](https://camo.githubusercontent.com/79cbbc1cbcdad74d8d05f144193020d25eb0ee5deed4a3486e7aa18df68ce75c/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f67616a75732f6469726563746f722f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/gajus/director?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/5176838fd81546aae8c32ac3a7848b34b0799324dae195a798d112322ffb5feb/68747470733a2f2f706f7365722e707567782e6f72672f67616a75732f6469726563746f722f76657273696f6e2e706e67)](https://packagist.org/packages/gajus/director)[![License](https://camo.githubusercontent.com/030a807c730359930f79ff204a068962558a3a66a068bac8f60dcded45f79655/68747470733a2f2f706f7365722e707567782e6f72672f67616a75732f6469726563746f722f6c6963656e73652e706e67)](https://packagist.org/packages/gajus/director)

Utility for generating URLs relative to the predefined routes and for handling the redirects.

Use case
--------

[](#use-case)

Use an instance of `Router` to generate URLs. It is convenient when your URL schema varies between the deployment environments.

URLs
----

[](#urls)

`Router` instance carries predefined routes that are used to construct URLs.

```
/**
 * @param string $url Default route URL.
 */
$locator = new \Gajus\Director\Locator('http://gajus.com/');

/**
 * @todo Check if query string is included.
 * @param string $route_name Route name.
 * @param string $url Absolute URL.
 * @return null
 */
$locator->setRoute('static', 'http://static.gajus.com/');
# null

/**
 * Get absolute URL using either of the predefined routes.
 * Requested resource path is appended to the route.
 *
 * @param string $path Relavite path to the route.
 * @param string $route Route name.
 */
$locator->url();
# http://gajus.com/

// Get URL relative to the default route:
$locator->url('post/1');
# http://gajus.com/post/1

// Get URL for the "static" route:
$locator->url(null, 'static');
# http://static.gajus.com/

// Get URL relative to the "static" route:
$locator->url('css/frontend.css', 'static');
# http://static.gajus.com/css/frontend.css
```

### Redirect

[](#redirect)

```
/**
 * Redirect user agent to the given URL.
 *
 * If no $url is provided, then attempt to redirect to the referrer
 * or (when referrer is not available) to the default route.
 *
 * @see http://benramsey.com/blog/2008/07/http-status-redirection/
 * @param string|null $url Absolute URL
 * @param int|null $response_code HTTP response code. Defaults to 303 when request method is POST, 302 otherwise.
 * @return null
 */
$locator->location();
# null (script execution terminated)

// Redirect to the default path with status code 307:
$locator->location(null, 307);
# null (script execution terminated)

// Redirect to an arbitrary URL:
$locator->location('http://gajus.com');
# null (script execution terminated)
```

`location` will throw `Exception\LogicException` exception if [headers have been already sent](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php).

### Get path

[](#get-path)

The iverse of the `url` method is `getPath`. It is used to get the resource path of the current request URI relative to a specific route:

```
// Taken from ./tests/RouterTest.php

$locator = new \Gajus\Director\Locator('https://gajus.com/foo/');

$_SERVER['HTTPS'] = 'on';
$_SERVER['HTTP_HOST'] = 'gajus.com';
$_SERVER['REQUEST_URI'] = '/foo/';

$this->assertSame('', $locator->getPath());

$_SERVER['HTTPS'] = 'on';
$_SERVER['HTTP_HOST'] = 'gajus.com';
$_SERVER['REQUEST_URI'] = '/foo/bar/';

$this->assertSame('bar/', $locator->getPath());

$_SERVER['HTTPS'] = 'on';
$_SERVER['HTTP_HOST'] = 'gajus.com';
$_SERVER['REQUEST_URI'] = '/foo/bar/?foo[bar]=1';

$this->assertSame('bar/', $locator->getPath());
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~86 days

Total

3

Last Release

4237d ago

Major Versions

0.1.0 → 1.0.02014-09-03

### Community

Maintainers

![](https://www.gravatar.com/avatar/b46b18abe058b038c677a9fa4aa6c39de7597196fb72a11add1f11600412a0e5?d=identicon)[gajus](/maintainers/gajus)

---

Top Contributors

[![gajus](https://avatars.githubusercontent.com/u/973543?v=4)](https://github.com/gajus "gajus (66 commits)")

---

Tags

urlredirect

### Embed Badge

![Health badge](/badges/gajus-director/health.svg)

```
[![Health](https://phpackages.com/badges/gajus-director/health.svg)](https://phpackages.com/packages/gajus-director)
```

###  Alternatives

[spatie/url

Parse, build and manipulate URL's

73914.3M97](/packages/spatie-url)[jbroadway/urlify

A fast PHP slug generator and transliteration library that converts non-ascii characters for use in URLs.

6737.4M62](/packages/jbroadway-urlify)[league/uri-components

URI components manipulation library

31932.3M67](/packages/league-uri-components)[sabre/uri

Functions for making sense out of URIs.

29335.2M40](/packages/sabre-uri)[spomky-labs/base64url

Base 64 URL Safe Encoding/Decoding PHP Library

15439.5M49](/packages/spomky-labs-base64url)[wazum/sluggi

TYPO3 extension for URL slug management with inline editing, auto-sync, locking, access control, and redirects

39488.5k](/packages/wazum-sluggi)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
