2017 © Pedro Peláez
 

library director

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

image

gajus/director

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

  • Saturday, December 31, 2016
  • by gajus
  • Repository
  • 2 Watchers
  • 1 Stars
  • 32 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Director

Build Status Coverage Status Latest Stable Version License, (*1)

Utility for generating URLs relative to the predefined routes and for handling the redirects., (*2)

Use case

Use an instance of Router to generate URLs. It is convenient when your URL schema varies between the deployment environments., (*3)

URLs

Router instance carries predefined routes that are used to construct URLs., (*4)

/**
 * @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 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., (*5)

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:, (*6)

// 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());

The Versions

31/12 2016

dev-master

9999999-dev https://github.com/gajus/director

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

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

The Development Requires

by Gajus Kuizinas

url redirect

06/10 2014

1.0.1

1.0.1.0 https://github.com/gajus/director

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

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

The Development Requires

by Gajus Kuizinas

url redirect

03/09 2014

1.0.0

1.0.0.0 https://github.com/gajus/director

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

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

The Development Requires

by Gajus Kuizinas

url redirect

16/04 2014

0.1.0

0.1.0.0 https://github.com/gajus/director

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

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

The Development Requires

by Gajus Kuizinas

url redirect