2017 © Pedro Peláez
 

library simple-router

A simple wildcard matcher

image

rkr/simple-router

A simple wildcard matcher

  • Monday, December 7, 2015
  • by rkr
  • Repository
  • 2 Watchers
  • 1 Stars
  • 32 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 7 % Grown

The README.md

php-simple-router

A simple, nearly unopinionated routing approach., (*1)

Workflow:, (*2)

1.) Define some routes. Routes are some arrays identified by a unique key. The data-part is completely up to you. If you want something special in you data-part, go on and define it there., (*3)

2.) Instantiate Router and pass the routes-array to it. The router does nothing more than lookup a data-array who's key is matching the incoming REQUEST_URI (for example). So the return-value of $router->lookup() is the value-part of the matching key., (*4)

3.) Do something with the data-array. For example, you can merge the $_GET- or $_POST-params into some data-key, do some post-processing, whatever., (*5)

4.) Optionally utilize a Dispatcher to call some arbitrary class., (*6)

This is what the bootstrap could look like:, (*7)

// Setup some test-request-parameters
$_SERVER['REQUEST_URI'] = '/some/path/10';
$_SERVER['REQUEST_METHOD'] = 'GET';

// Should get overwritten by the /10 above
$_GET['start'] = 20;

$_SERVER = array_merge(['REQUEST_URI' => '/', 'REQUEST_METHOD' => 'GET'], $_SERVER);

$routes = [
  'GET /' => ['class' => IndexCtrl::class, 'method' => 'start'],
  'GET /some/path' => ['class' => LoginCtrl::class, 'method' => 'test'],
  'GET /some/path/:start' => ['class' => 'UserCtrl::class', 'method' => 'test'],
  'GET /list:start?type=gallery' => ['class' => 'ProductsCtrl::class', 'method' => 'showGallery'],
];

$router = new Router($routes);
$data = $router->lookup($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD'], $_REQUEST);

print_r($data); // ['data' => ['class' => IndexCtrl::class, 'method' => 'start'], 'params' => []]

The Versions

07/12 2015

dev-master

9999999-dev https://github.com/rkrx/php-simple-router

A simple wildcard matcher

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ron Kirschler