amdp3-metaforums/index.php

67 lines
1.5 KiB
PHP
Raw Normal View History

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 != '') {
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)) {
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