2017 © Pedro Peláez
 

library spiffy-route

Spiffy\Route is a light-weight, HHVM compatible, and dependency free router library.

image

spiffy/spiffy-route

Spiffy\Route is a light-weight, HHVM compatible, and dependency free router library.

  • Monday, January 19, 2015
  • by SpiffyJr
  • Repository
  • 0 Watchers
  • 0 Stars
  • 571 Installations
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Spiffy\Route

Build Status Code Coverage Scrutinizer Code Quality, (*1)

Installation

Spiffy\Route can be installed using composer which will setup any autoloading for you., (*2)

composer require spiffy/spiffy-route, (*3)

Additionally, you can download or clone the repository and setup your own autoloading., (*4)

Adding routes

use Spiffy\Route\Router;

$router = new Router();

// Basic route with name
$router->add('foo', '/foo');
// matches /foo

// Basic route with no name
$router->add(null, '/foo');
// matches /foo

// Route with tokens
$router->add('foo', '/foo/{name}');
// matches /foo/bar

// Router with optional tokens
$router->add('foo', '/foo{/name?}');
// matches /foo or /foo/bar

// Router with tokens and constraints
$router->add('foo', '/foo/{id:\d+}-{slug}');
// matches /foo/1-bar but not /foo/baz-bar

// The kitchen sink
$router->add('foo', '/foo/{id:\d+}{-slug?:[a-zA-Z-_]+}');
// matches /foo/1, /foo/1-bar, /foo/1-BaR
// does not match /foo/1-2

Assembling named routes to url's

use Spiffy\Route\Router;

$router = new Router();
$router->add('foo', '/foo');

// outputs '/foo'
echo $router->assemble('foo');

$router->add('foo', '/foo/{id:\d+}{-slug?:[a-zA-Z-_]+}');

// outputs '/foo/1'
echo $router->assemble('foo', ['id' => 1]);

// outputs '/foo/1-bar'
echo $router->assemble('foo', ['id' => 1, 'slug' => 'bar']);

Matching routes

use Spiffy\Route\Router;

$router = new Router();
$router->add('foo', '/foo');

// result is NULL
echo $router->match('/bar');

// result is an instance of Spiffy\Route\RouteMatch
$match = $router->match('/foo');

// output is 'foo'
echo $match->getName();

$router->add('bar', '/bar/{id}');

// result is an instance of Spiffy\Route\RouteMatch
$match = $router->match('/bar/1');

// output is 'bar'
echo $match->getName();

// output is '1'
echo $match->get('id');

// you can also have defaults for params that may not exist (output is 'foo')
echo $match->get('does-not-exist', 'foo');

Default route parameters

```php use Spiffy\Route\Router;, (*5)

$router = new Router(); $router->add('foo', '/foo{/id?}', ['defaults' => ['id' => 1, 'controller' => 'foo-controller']]);, (*6)

$match = $router->match('/foo/1234');, (*7)

// output is '1234' echo $match->get('id');, (*8)

$match = $router->match('/foo');, (*9)

// output is '1' echo $match->get('id');, (*10)

// output is 'foo-controller' echo $match->get('controller');, (*11)

The Versions

19/01 2015

dev-master

9999999-dev

Spiffy\Route is a light-weight, HHVM compatible, and dependency free router library.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

The Development Requires

17/07 2014

1.0.0-alpha

1.0.0.0-alpha

Spiffy\Route is a light-weight, HHVM compatible, and dependency free router library.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

The Development Requires