Autoload Composer class not found

Asked

Viewed 1,437 times

0

Good afternoon!

I have a problem and do not know how to solve, creating a mini application, using the autoload of Composer, but is not working, is giving that class does not exist. This is my structure:

├── app
    └── Conn
        └── Conn.php
└── vendor
    └── composer
    └── autoload.php
└── composer.json
└── index.php
└── README.md

My class Conn ta thus:

<?php

namespace Lelvtex\Conec;

class Conn
{
    private $user;
    private $pass;
    private $dbsa;
    private $host;

    private $connon;
    private $conn;


    //CONSTRUCTOR

    public function __construct($user, $pass, $dbsa, $host)
    {
        $this->connect($user, $pass, $dbsa, $host);
    }


    //PRIVATE METHODS
    private function connect($user, $pass, $dbsa, $host)
    {
        $this->connon = false;
        $this->user = strip_tags(trim($user));
        $this->pass = strip_tags(trim($pass));
        $this->dbsa = strip_tags(trim($dbsa));
        $this->host = $host;


        try {
            if (!$this->connon) {
                $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbsa;
                $options = array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',);
                $this->conn = new \PDO($dsn, $this->user, $this->pass, $options);
                $this->connon = true;
                echo "Conectado com sucesso!";
            }
        } catch (\PDOException $e) {
            echo $e->getMessage();
        }

    }
}

Man index ta thus:

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Livre e Leve</title>
</head>
<body>
<?php
require 'vendor/autoload.php';

use Lelvtex\Conec\Conn;

$conn = new Conn('root', '', 'livreelevevtex', 'localhost');
var_dump($conn);
?>
</body>
</html>

Man autoload_psr4 ta thus:

<?php

// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'Lelvtex\\' => array($baseDir . '/app'),
);

Man composer.json ta thus:

{
    "name": "lucascar/livreelevevtex",
    "description": "Projeto de Teste para Livre e Leve vtex",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "Lucas de Carvalho",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "autoload" : {
        "psr-4" : {
            "Lelvtex\\" : "app/"
        }
    }
}

The mistake you’re making is this:

Fatal error: Class 'Lelvtex Conec Conn' not found in C: wamp64 www free projects

So everything seems to be okay, but I don’t know why it doesn’t work...

Show 2 more comments

2 answers

1

Lucas, try changing the namespace Conec (Class Conn) to the folder name in question (CONN). I don’t have the privilege of commenting yet. The namespace needs to be on the same path, otherwise it won’t find.

  • I saw the comment of the chat now, is the same as mine. Please inform that has already been answered the question. By the other user

0


My question was answered by the user @Jorge Costa no chat!

Lucas Gauna also gave practically the same explanation.

swap the namespace Conec (Class Conn) to the folder name in issue (CONN)

Thank you all!

Browser other questions tagged

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