-
-
Save wilcorrea/88626a7eaaa06a87c0ae29ba5d1e7de3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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