115 lines
3.2 KiB
PHP
Executable File
115 lines
3.2 KiB
PHP
Executable File
<?php
|
|
// Error reporting
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
// Start session
|
|
session_start();
|
|
|
|
// Set timezone
|
|
ini_set('date.timezone', 'Europe/Brussels');
|
|
|
|
// Autoload classes
|
|
spl_autoload_register(function ($class) {
|
|
$prefix = 'App\\';
|
|
$baseDir = __DIR__ . '/../src/';
|
|
|
|
$len = strlen($prefix);
|
|
if (strncmp($prefix, $class, $len) !== 0) {
|
|
return;
|
|
}
|
|
|
|
$relativeClass = substr($class, $len);
|
|
// Convert namespace separators to directory separators
|
|
$file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';
|
|
|
|
// Convert to lowercase for directory names
|
|
$parts = explode('/', $file);
|
|
$newParts = [];
|
|
foreach ($parts as $part) {
|
|
if (strpos($part, '.php') === false) {
|
|
$newParts[] = strtolower($part);
|
|
} else {
|
|
$newParts[] = $part;
|
|
}
|
|
}
|
|
$file = implode('/', $newParts);
|
|
|
|
if (file_exists($file)) {
|
|
require $file;
|
|
}
|
|
});
|
|
|
|
// Load configuration
|
|
$config = include __DIR__ . '/../config/app.php';
|
|
|
|
// Database configuration
|
|
$databaseConfig = include __DIR__ . '/../config/database.php';
|
|
|
|
// Set database environment variables for Docker
|
|
if (!getenv('DB_HOST')) {
|
|
putenv('DB_HOST=mysql');
|
|
}
|
|
if (!getenv('DB_NAME')) {
|
|
putenv('DB_NAME=proostenkrant');
|
|
}
|
|
if (!getenv('DB_USER')) {
|
|
putenv('DB_USER=proostenkrant');
|
|
}
|
|
if (!getenv('DB_PASSWORD')) {
|
|
putenv('DB_PASSWORD=proostenkrant');
|
|
}
|
|
|
|
// Load routes
|
|
$routes = include __DIR__ . '/../config/routes.php';
|
|
|
|
// Get current request
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
|
|
|
// Route matching
|
|
$matched = false;
|
|
foreach ($routes[$method] ?? [] as $pattern => $handler) {
|
|
// Convert route pattern to regex
|
|
// First, replace route parameter patterns before quoting
|
|
$patternForRegex = preg_replace('/\(\d+\)/', '(\d+)', $pattern);
|
|
$patternForRegex = preg_replace('/\(\w+\)/', '([^\/]+)', $patternForRegex);
|
|
$regex = '#^' . $patternForRegex . '$#';
|
|
|
|
if (preg_match($regex, $uri, $matches)) {
|
|
// Remove first match (full match)
|
|
array_shift($matches);
|
|
|
|
// Parse handler
|
|
list($controllerName, $actionName) = explode('@', $handler);
|
|
|
|
// Instantiate controller
|
|
$controllerClass = 'App\\Controllers\\' . $controllerName;
|
|
if (class_exists($controllerClass)) {
|
|
$controller = new $controllerClass();
|
|
|
|
// Call action with parameters
|
|
if (method_exists($controller, $actionName)) {
|
|
call_user_func_array([$controller, $actionName], $matches);
|
|
$matched = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle 404
|
|
if (!$matched) {
|
|
http_response_code(404);
|
|
|
|
// Show a simple 404 page
|
|
echo '<!DOCTYPE html>';
|
|
echo '<html lang="nl">';
|
|
echo '<head><meta charset="UTF-8"><title>404 - Pagina niet gevonden</title></head>';
|
|
echo '<body style="text-align: center; padding: 50px;">';
|
|
echo '<h1>404 - Pagina niet gevonden</h1>';
|
|
echo '<p><a href="/">Ga terug naar de homepagina</a></p>';
|
|
echo '</body></html>';
|
|
}
|