2019-11-17 19:22:25 +00:00
|
|
|
<?php
|
2019-11-18 13:33:45 +00:00
|
|
|
|
|
|
|
require 'autoload.php';
|
|
|
|
|
|
|
|
// Use helper classes from Application
|
|
|
|
use Application\HTTP\Request;
|
|
|
|
use Application\HTTP\Response;
|
|
|
|
use Application\Services\ServiceContainer;
|
|
|
|
|
|
|
|
ServiceContainer::Database();
|
|
|
|
ServiceContainer::Session();
|
|
|
|
|
|
|
|
// Get all routes
|
|
|
|
$routes = require 'routes.php';
|
|
|
|
|
2019-11-17 19:22:25 +00:00
|
|
|
// Get request URI
|
|
|
|
$uri = $_SERVER['PHP_SELF'];
|
2019-11-18 13:33:45 +00:00
|
|
|
|
2019-11-17 19:22:25 +00:00
|
|
|
// Cut off index.php
|
|
|
|
$uri = substr($uri,strlen('/index.php'),strlen($uri)-strlen('/index.php'));
|
2019-11-18 13:33:45 +00:00
|
|
|
|
|
|
|
// Serve static files first
|
|
|
|
if(file_exists('Application/Static'.$uri) && $uri != '') {
|
|
|
|
readfile('Application/Static'.$uri);
|
|
|
|
exit;
|
2019-11-17 19:22:25 +00:00
|
|
|
}
|
2019-11-18 13:33:45 +00:00
|
|
|
|
|
|
|
// Remove trailing slash
|
|
|
|
if(substr($uri,strlen($uri)-1,1) == '/') {
|
|
|
|
$uri = substr($uri,0,strlen($uri)-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build request object to pass to controller
|
|
|
|
$request = new Request();
|
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
|
|
|
|
$request_method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
|
|
|
|
// Get current route from uri
|
|
|
|
if(!array_key_exists($request_method.':'.$uri,$routes)) {
|
|
|
|
http_response_code(404);
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
die(json_encode([ 'success' => false, 'error' => 'Not found' ]));
|
|
|
|
}
|
|
|
|
|
|
|
|
$route = $routes[$request_method.':'.$uri];
|
|
|
|
|
|
|
|
// Duar (actually, split the method string to class name and method name)
|
|
|
|
$method_part = explode("@",$route['controller']);
|
|
|
|
|
|
|
|
// Get class name and method name
|
|
|
|
$class = $method_part[0];
|
|
|
|
$method = $method_part[1];
|
|
|
|
|
|
|
|
// Get fully qualified class name of route
|
|
|
|
$fqcn = 'Application\\Controllers\\'.$class;
|
|
|
|
$controller = new $fqcn();
|
|
|
|
|
|
|
|
$response = $controller->$method($request,$response);
|
|
|
|
|
|
|
|
$response->render();
|
|
|
|
|
|
|
|
// Convert array to JSON
|
|
|
|
|