Good afternoon!
I have a problem and I do not know how to solve it, so creating a mini application, using the autoload of the composer, but it is not working, it is giving the 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
looks like this:
<?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();
}
}
}
My index
looks like this:
<!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>
My autoload_psr4
looks like this:
<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Lelvtex\' => array($baseDir . '/app'),
);
My composer.json
looks like this:
{
"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 error you are giving is this:
Fatal error: Class 'Lelvtex \ Conec \ Conn' not found in C: \ wamp64 \ www \ projects \ livreelevevtex \ index.php on line 13
So, everything seems ok, but I do not know why it does not work ...