Skip to content

Instantly share code, notes, and snippets.

@tomodutch
Last active August 29, 2015 14:04
Show Gist options
  • Save tomodutch/b7269d421c6ee80545ae to your computer and use it in GitHub Desktop.
Save tomodutch/b7269d421c6ee80545ae to your computer and use it in GitHub Desktop.
<?php
require_once('vendor/autoload.php');
use TFarla\Routing\Router;
use TFarla\Routing\Route;
$router = new Router([
new Route('show user', '^/users/(?P<id>\d+)/$', ['GET'], function($matches) {
return $matches['id'];
}),
new Route('list users', '^/users/$', ['GET'], function($matches) {
return 'users';
}),
new Route('index', '^/$', ['GET'], function($matches) {
return 'index';
}),
new Route('404', '', ['GET'], function($matches) {
return '404 - not found';
}),
]);
$uri = $_SERVER['REQUEST_URI'];
$method = $_SERVER['REQUEST_METHOD'];
try {
list($route, $matches) = array_values($router->match($uri, $method));
echo $route($matches);
} catch (\TFarla\Routing\RoutingException $routingException) {
die('No Route found.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment