Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active May 18, 2020 22:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/70ffe112579adbbfaafce36646971dbd to your computer and use it in GitHub Desktop.
Save parzibyte/70ffe112579adbbfaafce36646971dbd to your computer and use it in GitHub Desktop.
<?php
require __DIR__ . '/vendor/autoload.php'; #Cargar todas las dependencias
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
use Phroute\Phroute\Exception\HttpRouteNotFoundException;
use Phroute\Phroute\Exception\HttpMethodNotAllowedException;
$collector = new RouteCollector();
$collector->get("/", function(){
return "Esta es la raíz";
});
$despachador = new Dispatcher($collector->getData());
$rutaCompleta = $_SERVER["REQUEST_URI"];
$metodo = $_SERVER['REQUEST_METHOD'];
$rutaLimpia = processInput($rutaCompleta);
try {
echo $despachador->dispatch($metodo, $rutaLimpia); # Mandar sólo el método y la ruta limpia
} catch (HttpRouteNotFoundException $e) {
echo "Error: Ruta no encontrada";
} catch (HttpMethodNotAllowedException $e) {
echo "Error: Ruta encontrada pero método no permitido";
}
/**
* Gracias a https://www.sitepoint.com/fast-php-routing-phroute/
*/
function processInput($uri)
{
return implode('/',
array_slice(
explode('/', $uri), 3));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment