<?php
require_once("config/config.php");

$ruta = !empty($_GET['url']) ? $_GET['url'] : "home/index";
$array = explode("/", $ruta);

$controller = $array[0];
$metodo = "index";
$parametro = "";

/* METODO */
if (!empty($array[1])) {
    $metodo = $array[1];
}

/* PARAMETROS */
if (!empty($array[2])) {
    for ($i = 2; $i < count($array); $i++) {
        $parametro .= $array[$i] . ",";
    }
    $parametro = trim($parametro, ",");
}

require_once("config/app/autoload.php");

$dircontrollers = "controllers/" . $controller . ".php";

if (file_exists($dircontrollers)) {

    require_once($dircontrollers);

    $controller = new $controller();

    if (method_exists($controller, $metodo)) {

        $controller->$metodo($parametro);

    } else {

        header("Location: " . base_url . "errors");
        exit;

    }

} else {

    header("Location: " . base_url . "errors");
    exit;

}