MAPG-86 adapt index.php to the new routing structure

This commit is contained in:
Bence Pőcze 2020-05-31 20:44:45 +02:00
parent 44db217e8b
commit b2eabfb9b8

View File

@ -4,36 +4,36 @@ require '../main.php';
// very basic routing // very basic routing
$host = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME']; $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) { if (($pos = strpos($url, '?')) !== false) {
$url = substr($url, 0, $pos); $url = substr($url, 0, $pos);
} }
switch($url) { $url = rawurldecode($url);
case '/maps':
$controller = new MapGuesser\Controller\MapsController(); Container::$routeCollection->get('index', '', [MapGuesser\Controller\HomeController::class, 'getIndex']);
break; Container::$routeCollection->get('maps', 'maps', [MapGuesser\Controller\MapsController::class, 'getMaps']);
case '/game': Container::$routeCollection->group('game', function (MapGuesser\Routing\RouteCollection $routeCollection) {
$mapId = isset($_GET['map']) ? (int) $_GET['map'] : 0; $routeCollection->get('game', '{mapId}', [MapGuesser\Controller\GameController::class, 'getGame']);
$controller = new MapGuesser\Controller\GameController($mapId); $routeCollection->get('game-json', '{mapId}/json', [MapGuesser\Controller\GameController::class, 'getGameJson']);
break; $routeCollection->get('position-json', '{mapId}/position.json', [MapGuesser\Controller\PositionController::class, 'getPosition']);
case '/game.json': $routeCollection->post('guess-json', '{mapId}/guess.json', [MapGuesser\Controller\PositionController::class, 'evaluateGuess']);
$mapId = isset($_GET['map']) ? (int) $_GET['map'] : 0; });
$controller = new MapGuesser\Controller\GameController($mapId, true);
break; $match = Container::$routeCollection->match($method, explode('/', $url));
case '/position.json':
$mapId = isset($_GET['map']) ? (int) $_GET['map'] : 0; if ($match !== null) {
$controller = new MapGuesser\Controller\PositionController($mapId); list($route, $params) = $match;
break;
case '/': $response = $route->callController($params);
header('Location: ' . $host . '/maps', true, 302);
die; if ($response instanceof MapGuesser\Interfaces\Response\IContent) {
default: header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
echo 'Error 404'; echo $response->render();
die; } 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();