Last active
May 18, 2020 22:16
-
-
Save parzibyte/70ffe112579adbbfaafce36646971dbd 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'; #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