2019-11-17 19:22:25 +00:00
|
|
|
<?php
|
2019-11-18 13:33:45 +00:00
|
|
|
|
|
|
|
require 'autoload.php';
|
|
|
|
|
2019-11-19 03:36:39 +00:00
|
|
|
date_default_timezone_set('Asia/Jakarta');
|
|
|
|
|
2019-11-18 13:33:45 +00:00
|
|
|
// 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 != '') {
|
2019-11-20 05:19:02 +00:00
|
|
|
$extension = pathinfo('Application/Static'.$uri, PATHINFO_EXTENSION);
|
|
|
|
if($extension == "js") {
|
|
|
|
header("Content-Type: text/javascript");
|
|
|
|
} else if($extension == "css") {
|
|
|
|
header("Content-Type: text/css");
|
|
|
|
} else {
|
|
|
|
header("Content-Type: ".mime_content_type('Application/Static'.$uri));
|
|
|
|
}
|
2019-11-18 13:33:45 +00:00
|
|
|
readfile('Application/Static'.$uri);
|
|
|
|
exit;
|
2019-11-17 19:22:25 +00:00
|
|
|
}
|
2019-11-18 13:33:45 +00:00
|
|
|
|
2019-11-20 05:19:02 +00:00
|
|
|
if(file_exists('Application/Storage'.$uri) && $uri != '') {
|
|
|
|
$extension = pathinfo('Application/Storage'.$uri, PATHINFO_EXTENSION);
|
|
|
|
if($extension == "js") {
|
|
|
|
header("Content-Type: text/javascript");
|
|
|
|
} else if($extension == "css") {
|
|
|
|
header("Content-Type: text/css");
|
|
|
|
} else {
|
|
|
|
header("Content-Type: ".mime_content_type('Application/Storage'.$uri));
|
|
|
|
}
|
|
|
|
readfile('Application/Storage'.$uri);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
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)) {
|
2019-11-19 03:36:39 +00:00
|
|
|
$response->statusCode(404)->view('404')->render();
|
|
|
|
die();
|
2019-11-18 13:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
|