MAPG-3 create basic contoller functionality

This commit is contained in:
Bence Pőcze 2020-05-17 22:30:22 +02:00
parent c63f05295e
commit 555984caf5
5 changed files with 70 additions and 1 deletions

View File

@ -2,7 +2,9 @@
require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
const ROOT = __DIR__;
$dotenv = Dotenv\Dotenv::createImmutable(ROOT);
$dotenv->load();
if (!empty($_ENV['DEV'])) {

5
public/.htaccess Normal file
View File

@ -0,0 +1,5 @@
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

16
public/index.php Normal file
View File

@ -0,0 +1,16 @@
<?php
require '../main.php';
// very basic routing
$url = $_SERVER['REQUEST_URI'];
switch($url) {
case '/':
$controller = new MapGuesser\Controller\GuessController();
break;
default:
echo 'Error 404';
die;
}
echo $controller->render();

View File

@ -0,0 +1,24 @@
<?php namespace MapGuesser\Controller;
abstract class BaseController
{
protected string $view;
protected array $variables = [];
public function render() : string
{
$this->operate();
extract($this->variables);
ob_start();
require ROOT . '/views/' . $this->view . '.php';
$content = ob_get_contents();
ob_end_clean();
return $content;
}
abstract protected function operate() : void;
}

View File

@ -0,0 +1,22 @@
<?php namespace MapGuesser\Controller;
use MapGuesser\Util\Geo\Bounds;
use MapGuesser\Util\Geo\Position;
class GuessController extends BaseController
{
protected string $view = 'guess';
protected function operate() : void
{
// demo position
$realPosition = new Position(47.85239, 13.35101);
// demo bounds
$bounds = new Bounds($realPosition);
$bounds->extend(new Position(48.07683,7.35758));
$bounds->extend(new Position(47.57496, 19.08077));
$this->variables = compact('realPosition', 'bounds');
}
}