Problem with PHP routes

0

I have a problem with the routes (I think) since I am working with Xampp in Windows and the project works well, but when uploading it to a hosting, Mark me an error:

  

Fatal error: Class '\ App \ Controllers \ Home' not found in /home/liftechc/public_html/rrhh/Core/App.php on line 53

The App.php file contains this snippet of code:

/**
* @var
*/
const NAMESPACE_CONTROLLERS = "\App\Controllers\";

/**
 * @var
 */
const CONTROLLERS_PATH = "../App/controllers/";

/**
 * [__construct description]
 */
public function __construct() {
    //obtenemos la url parseada
    $url = $this->parseUrl();

    //Comprobamos que exista el archivo en el directorio controllers.
    if(file_exists(self::CONTROLLERS_PATH . ucfirst($url[0]) . ".php")) {
        //nombre del archivo a llamar
        $this->_controller = ucfirst($url[0]);
        //eliminamos el controlador de url,
        // así sólo nos quedarán los parámetros del método
        unset($url[0]);
    } else {
        include APPPATH . "/views/errors/404.php";
        exit;
    }
    //Obtenemos la clase con su espacio de nombres.
    $fullClass = self::NAMESPACE_CONTROLLERS . $this->_controller;

    //Asociamos la instancia a $this->_controller
    $this->_controller = new $fullClass; <--- ESTA ES LA LINEA 53, DONDE ME DA EL ERROR!

    //Si existe el segundo segmento comprobamos que el método exista en esa clase.
    if(isset($url[1])) {

        //aquí tenemos el método
        $this->_method = $url[1];
        if(method_exists($this->_controller, $url[1])) {
            //Eliminamos el método de url,
            // así sólo nos quedarán los parámetros del método.
            unset($url[1]);
        } else {
            throw new \Exception("Error Processing Method {$this->_method}", 1);
        }
    }
    //Asociamos el resto de segmentos a $this->_params
    // para pasarlos al método llamado, por defecto será un array vacío.
    $this->_params = $url ? array_values($url) : [];
}

The index.php file contains this code:

//Borrar para produccion
error_reporting(E_ALL);
ini_set('display_errors', 1);

//Sesion
session_start();

//URL
define("DIR_URL", "http://localhost/rrhh2/public/");
//define("DIR_URL", "http://sitios-en-desarrollo.com/rrhh/public/");

//Directorio del proyecto.
define("PROJECTPATH", dirname(__DIR__));

//Directorio app
define("APPPATH", PROJECTPATH . '/App');

//autoload con namespaces
function autoload_classes($class_name) {
    $filename = PROJECTPATH . '/' . str_replace('\', '/', $class_name) . '.php';
    if(is_file($filename)) {
        include_once $filename;
    }
}
spl_autoload_register('autoload_classes'); //Registramos el autoload autoload_classes
$app = new \Core\App; //Instanciamos la app
$app->render(); //Lanzamos la app
    
asked by Rafael Moreno 14.03.2017 в 04:06
source

1 answer

0

In __construct () you do file_exists with:

const CONTROLLERS_PATH = "../App/controllers/";

When you check if the file exists, it tells you that if xq the "controllers" folder is in lowercase.

../App/controllers/Home.php

In the "autoload_classes" function you make is_file with the name of the class built with:

const NAMESPACE_CONTROLLERS = "\App\Controllers\";

there, the word "Controllers" is with the first letter in capital letters

"autoload_classes" just change the Namespace bars by Directory Separator, but execute is_file with:

../App/Controllers/Home.php

that does not exist ....

Linux is case-sensitive.

I hope I have helped this time ...

    
answered by 14.03.2017 / 19:35
source