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...
namespace App Conn;
– Jorge Costa
Why App Conn, Jorge?
– Lucas de Carvalho
Composer maps the namespace to the path for the file
– Jorge Costa
Can you explain in a reply? In case I am using the autoload of Composer.
– Lucas de Carvalho
Please put the Composer.json
– Jorge Costa
Jorge, I put it in the edition
– Lucas de Carvalho
Let’s go continue this discussion in chat.
– Jorge Costa