From b2eabfb9b845ef352398ab7c0a5633cd0941a426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 31 May 2020 20:44:45 +0200 Subject: [PATCH] MAPG-86 adapt index.php to the new routing structure --- public/index.php | 58 ++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/public/index.php b/public/index.php index 0d592cc..681a0d9 100644 --- a/public/index.php +++ b/public/index.php @@ -4,36 +4,36 @@ require '../main.php'; // very basic routing $host = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME']; -$url = $_SERVER['REQUEST_URI']; +$method = strtolower($_SERVER['REQUEST_METHOD']); +$url = substr($_SERVER['REQUEST_URI'], strlen('/')); if (($pos = strpos($url, '?')) !== false) { $url = substr($url, 0, $pos); } -switch($url) { - case '/maps': - $controller = new MapGuesser\Controller\MapsController(); - break; - case '/game': - $mapId = isset($_GET['map']) ? (int) $_GET['map'] : 0; - $controller = new MapGuesser\Controller\GameController($mapId); - break; - case '/game.json': - $mapId = isset($_GET['map']) ? (int) $_GET['map'] : 0; - $controller = new MapGuesser\Controller\GameController($mapId, true); - break; - case '/position.json': - $mapId = isset($_GET['map']) ? (int) $_GET['map'] : 0; - $controller = new MapGuesser\Controller\PositionController($mapId); - break; - case '/': - header('Location: ' . $host . '/maps', true, 302); - die; - default: - echo 'Error 404'; - die; +$url = rawurldecode($url); + +Container::$routeCollection->get('index', '', [MapGuesser\Controller\HomeController::class, 'getIndex']); +Container::$routeCollection->get('maps', 'maps', [MapGuesser\Controller\MapsController::class, 'getMaps']); +Container::$routeCollection->group('game', function (MapGuesser\Routing\RouteCollection $routeCollection) { + $routeCollection->get('game', '{mapId}', [MapGuesser\Controller\GameController::class, 'getGame']); + $routeCollection->get('game-json', '{mapId}/json', [MapGuesser\Controller\GameController::class, 'getGameJson']); + $routeCollection->get('position-json', '{mapId}/position.json', [MapGuesser\Controller\PositionController::class, 'getPosition']); + $routeCollection->post('guess-json', '{mapId}/guess.json', [MapGuesser\Controller\PositionController::class, 'evaluateGuess']); +}); + +$match = Container::$routeCollection->match($method, explode('/', $url)); + +if ($match !== null) { + list($route, $params) = $match; + + $response = $route->callController($params); + + if ($response instanceof MapGuesser\Interfaces\Response\IContent) { + header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8'); + echo $response->render(); + } elseif ($response instanceof MapGuesser\Interfaces\Response\IRedirect) { + header('Location: ' . $host . '/' . $response->getUrl(), true, $response->getHttpCode()); + } +} else { + header('Content-Type: text/html; charset=UTF-8', true, 404); + require ROOT . '/views/error/404.php'; } - -$view = $controller->run(); - -header('Content-Type: ' . $view->getContentType() . '; charset=UTF-8'); - -echo $view->render();