Require_once is unable to access file

Asked

Viewed 3,183 times

0

I’m not being able to include pages in my file controle_login.php, as it generates the following errors:

Notice: Use of Undefined Constant ABSPATH - assumed 'ABSPATH' in C: xampp htdocs Modelo_mvc controlles controle_login.php on line 2

Warning: require_once(ABSPATH/models/model_login): failed to open stream: No such file or directory in C: xampp htdocs Modelo_mvc controlles controle_login.php on line 2

Fatal error: require_once(): Failed Opening required 'ABSPATH/models/model_login' (include_path='C: xampp php PEAR') in C: xampp htdocs Modelo_mvc controlles controle_login.php on line 2

I’m gonna post the codes 'cause there’s weeks that I can’t figure out what’s going on.

config.php file

inserir a descrição da imagem aqui

index.php file inserir a descrição da imagem aqui

Filing cabinet controle_login.php (this file is inside a folder called controlles)

<?php
require_once ABSPATH.'/models/model_login.php';

class Controlador extends Controle_login{

    public function __construct(){
        $obj = new Controle_login;
    }

Filing cabinet model_login.php (this file is inside the models folder)

<?php
require_once ABSPATH.'conexao.php';

class Controle_login{

    private $con;

    public function __construct {
        $con = new Conexao();
    }

    public function controle_acesso($usuario, $senha){
        $this->setUsuario($usuario);
        $this->setSenha($senha);

        $consulta = $this->con->conectar()->prepare("SELECT COUNT(*) AS total FROM login WHERE usuario=:usuario AND senha=:senha");
        $consulta->bindParam(":usuario", $this->usuario);
        $consulta->bindParam(":senha", $this->senha);
        $consulta->execute();
        $linha = $consulta->fetch(PDO::FETCH_ASSOC);
        return $linha['total'];
    }

}

Folder structure: inserir a descrição da imagem aqui

1 answer

3


The error occurs because the variable ABSPATH is being declared inside the archive config.php, so for you to use it inside other files it is necessary to load the file config.php before.

Like your folders controllers and models are in the same directory as the file config.php you can import the following before using the constant ABSPATH:

require_once dirname(__FILE__)."/../config.php";

For example, your file controle_login.php would look like this:

<?php
require_once dirname(__FILE__)."/../config.php";
require_once ABSPATH.'/models/model_login.php';

class Controlador extends Controle_login{

    public function __construct(){
        $obj = new Controle_login;
    }

OBS: The ..(two points) in the directory path has the function of return a folder.

  • I did exactly as you explained and it worked :) Thanks for your help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.