<?php
require_once 'config/config.php';

$url = isset($_GET['url']) ? $_GET['url'] : '';
$url = rtrim($url, '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);

if (empty($url[0])) {
    $controller = 'HomeController';
    $method = 'index';
} else {
    $controller = ucfirst($url[0]) . 'Controller';
    $method = isset($url[1]) ? $url[1] : 'index';
}

if (file_exists('controllers/' . $controller . '.php')) {
    require_once 'controllers/' . $controller . '.php';
    $controller = new $controller();
    
    if (method_exists($controller, $method)) {
        $params = array_slice($url, 2);
        call_user_func_array([$controller, $method], $params);
    } else {
        echo "Method not found!";
    }
} else {
    echo "Controller not found!";
}
