0
My structure
localhost/sistema
localhost/sistema/.htaccess
localhost/sistema/composer.json
localhost/sistema/composer.lock
localhost/sistema/composer.phar
localhost/sistema/App
localhost/sistema/App/mvc/
localhost/sistema/App/mvc/Controles
localhost/sistema/App/mvc/Controles/Planos.php
localhost/sistema/App/mvc/Modelos
localhost/sistema/App/mvc/Modelos/Planos.php
localhost/sistema/App/mvc/Vistas
localhost/sistema/App/mvc/Vistas/Conexao.php
localhost/sistema/App/mvc/Vistas/Planos.php
localhost/sistema/vendor
localhost/sistema/public
localhost/sistema/public/index.php
Plans.php Controls
<?php
namespace CONTROLES;
use MODELOS\Planos;
class Planos {
private $conexao;
public function __construct ($_conexao) {
$this->conexao = $_conexao;
}
public function alteraPlano ($plano) {
$string = "UPDATE planos
SET nome ='".$plano->getNome()."',
descricao = '".$plano->getDescricao()."'
WHERE idPlano = ".$plano->getIdPlano();
return $this->conexao->query($string);
}
public function excluir ($idPlano) {
// CHAVE ESTRANGEIRA E ON DELETE CASCADE ATIVADO NO BANCO
$string = "DELETE FROM planos WHERE idPlano = ".$idPlano;
/*
DA FORMA ABAIXO É SOMENTE QUANDO NÃO CONFIGUROU O DELETE CASCADE NO BANCO
$string = "DELETE planos, fotos FROM planos
LEFT JOIN fotos ON
planos.idPlano = fotos.idPlano
WHERE planos.idPlano = ".$idPlano;
*/
return $this->conexao->query($string);
}
public function cadastrar ($plano) {
$string = "INSERT INTO planos (
nome,
descricao
)
VALUES (
'".$plano->getNome()."',
'".$plano->getDescricao()."'
)
";
return $this->conexao->query($string);
}
public function ultimoIdCadastrado () {
return $this->conexao->insert_id;
}
public function pesquisaPlano($idPlano) {
$plano = null;
$string = "SELECT
idPlano,
nome,
descricao
FROM planos
WHERE idPlano = ".$idPlano;
$registros = $this->conexao->query($string);
$quantasLinhas = $registros->num_rows;
if ($quantasLinhas > 0) {
list ($idPlano, $nome, $descricao) = $registros->fetch_row();
$plano = new Planos($nome, $descricao);
$plano->setIdPlano($idPlano);
}
return $plano;
}
public function pesquisaPlanos($where = NULL) {
$planos = null;
$string = "SELECT idPlano, nome, descricao FROM planos ".$where;
$registros = $this->conexao->query($string);
$quantasLinhas = $registros->num_rows;
if ($quantasLinhas > 0) {
while (list ($idPlano, $nome, $descricao) = $registros->fetch_row()) {
$plano = new Planos($nome, $descricao);
$plano->setIdPlano($idPlano);
$planos[] = $plano;
}
}
return $planos;
}
}
?>
Planos.php Modelos
<?php
namespace MODELOS;
class Planos {
private $idPlano;
private $nome;
private $descricao;
public function __construct (
$_nome,
$_descricao) {
$this->nome = $_nome;
$this->descricao = $_descricao;
}
public function setIdPlano ($_idPlano) {
$this->idPlano = $_idPlano;
}
public function getIdPlano () {
return $this->idPlano;
}
public function getNome () {
return $this->nome;
}
public function getDescricao() {
return $this->descricao;
}
}
?>
Planos.php Vistas
<?php
namespace VISTAS;
use CONTROLES\Planos as PC;
use MODELOS\Planos as PM;
class Planos {
private $conexao;
public function __construct ($_conexao) {
$this->conexao = $_conexao;
}
public function getPlanos () {
$planos = new PC($this->conexao);
$todos = $planos->pesquisaPlanos();
foreach ($todos as $plano) :
echo $plano->getNome()."<br />";
endforeach;
}
}
index.php public
<?php
ini_set("display_errors",true);
ini_set("display_startup_erros",1);
error_reporting(E_ALL && E_NOTICE && E_STRICT);
require_once "../App/mvc/Vistas/Conexao.php";
require_once "../vendor/autoload.php";
$conecta = new Conexao();
$conexao = $conecta->abreConexao();
$planos = new Planos($conexao);
print_r($planos->getPlanos());
?>
and Composer.json
{
"require" : {
"php" : "^5.5 || ^7.0",
"ext-mbstring": "*"
},
"authors": [
{
"name": "Carlcleo",
"email": "[email protected]",
"role": "Desenvolvedor"
}],
"autoload": {
"classmap": ["App"]
}
}
When I spin index.php
of server error.
Fatal error: Uncaught Error: Class 'Planos' not found in C:\Program Files\Apache24\Apache24\htdocs\mvc\public\index.php:14 Stack trace: #0 {main} thrown in C:\Program Files\Apache24\Apache24\htdocs\mvc\public\index.php on line 14
Where am I going wrong?
Can accompany in:
http://funerariasaopedro.net.br/mvc
Adding autoload_classmap:
<?php
// autoload_classmap.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'CONTROLES\\Planos' => $baseDir . '/App/mvc/Controles/Planos.php',
'Conexao' => $baseDir . '/App/mvc/Vistas/Conexao.php',
'MODELOS\\Planos' => $baseDir . '/App/mvc/Modelos/Planos.php',
'VISTAS\\Planos' => $baseDir . '/App/mvc/Vistas/Planos.php',
);
The class is in a namespace that you did not put in calling it. And if you’re using Autopload from Poser, you don’t have to import the files manually.
– Woss
Ok, after the fix, the error went to the Plans.php class that is in the Views folder that says Fatal error: Uncaught Error: Class 'CONTROLS Plans' not found in C:. Note: this class is now starting like this: <?php namespace VISTAS; use PC Flat CONTROLS; use PM Flat TEMPLATES;
– Carlos Rocha
Check the Namespaces in uppercase, because php is case sensitive. FLAT TEMPLATES is different from flat templates, see the paths, because if there is a folder named minuscule there could be problems in case of linux use.
– Jean Carlo
is all hitting correctly. That’s the problem. Adding autoload_classmap
– Carlos Rocha
Spun
composer dump
?– Wallace Maxters
yes. same way. I added autoload_classmap at the end of the question
– Carlos Rocha