Skip to content

Instantly share code, notes, and snippets.

@wilcorrea
Forked from raingrave/index.php
Last active August 20, 2018 18:08
Show Gist options
  • Save wilcorrea/88626a7eaaa06a87c0ae29ba5d1e7de3 to your computer and use it in GitHub Desktop.
Save wilcorrea/88626a7eaaa06a87c0ae29ba5d1e7de3 to your computer and use it in GitHub Desktop.
<?php
require __DIR__ . '/../vendor/autoload.php';
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $router) {
$router->get('/products', function(){
return 'This route responds to requests with the GET method at the path /example';
});
});
$method = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];
if (false !== $pos = strpos($uri, '?')) {
$uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);
$routeInfo = $dispatcher->dispatch($method, $uri);
switch ($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
echo '404 Not Found';
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$allowedMethods = $routeInfo[1];
echo '405 Method Not Allowed';
break;
case FastRoute\Dispatcher::FOUND:
$handler = $routeInfo[1];
$vars = $routeInfo[2];
echo $handler($vars);
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment