Problem php routes

0

Hi, I'm fighting a while ago with the php route system but I still have the same problem. The classes work correctly, the problem is when I call the class from the controller, the class route I call works, but the routes that class calls are not found, if it is better understood with the code and the error:

Controller:

    <?php

require_once '../models/DAO/HermandadDAO.php';

$accion = $_REQUEST['accion'];
switch ($accion) {
    case "insertar":
        $dao = new HermandadDAO();
        $values = array(
            "nombre" => $_REQUEST['nombre']
        );
        $dao->insertar($values);
        break;
}

DAO brotherhood class:

<?php

require_once '../entities/Hermandad.php';

require_once 'AbstractDAO.php';

class HermandadDAO extends AbstractDAO {

    public function insertar($arrayValues) {
        $values = array(
            Hermandad::NOMBRE => $arrayValues["nombre"]
        );
        return parent::insertar(Hermandad::TABLA, $values);
    }

}

AbstractDAO Class:

<?php

require_once '../../database/Conexion.php';

class AbstractDAO {
    private $conexion;
    private $dbHandler;

    public function __construct() {
        $this->conexion = new Conexion();
        $this->dbHandler = $this->conexion->getConexion();
        if (!$this->dbHandler) {
            echo "Error al conectar con la base de datos";
        }
    }

    public function insertar($tabla, $values) {
        $sql = "";
        $sql .= "INSERT INTO " . $tabla;
        $sql .= " (" . implode(",", array_keys($values)) . ") ";
        $sql .= "VALUES ('" . implode("','", array_values($values)) . "');";
        $query = mysqli_query($this->dbHandler, $sql);
        if ($query) {
          return true;
        } else {
           return false;
        }
    }
}

and then the error says that the class brotherhoodDAO does not find the class Brotherhood, but only happens to me calling it from the controller, when I try it from the brotherhoodDAO if it works.

Project structure:

    
asked by kmilo93sd 26.12.2017 в 00:20
source

2 answers

0

Suppose we are working on folderCreate.php which is in the mkdir folder

folderCreate.php you have createFile.php which is included: include '../users/createfile.php';

And createFile.php includes a x file in haider . would have include '/haider/x.php';

as we are in folderCreate.php We will not be able to access the file that createFile.php needs, due to its include '/haider/x.php'; because folderCreate.php is in mkdir and tries to perform the search from that point and does not fit the route.

Solution

It is a route orientation topic in the code.

Give the controller the require_once '../database/Conexion.php';

and AbstractDAO that does not have require_once '../../database/Conexion.php'; .

DAO Brotherhood change to require_once './models/entities/Hermandad.php';

with that it should work but it is not the right way

    
answered by 26.12.2017 в 01:50
0

From my point of view, the best way to avoid these problems is to define the routes in a qualified way, that is, to use complete routes instead of relative ones.

For example PHP provides $_SERVER['DOCUMENT_ROOT'] to place us in the root, from there we can define the challenge of the route, example:

 include($_SERVER['DOCUMENT_ROOT'].'/directorio/archivo.php');
    
answered by 26.12.2017 в 12:58