Problems with the Composer

Asked

Viewed 61 times

0

To learning to use the standard Composer psr-4, the class is not being found.

My index where the user class is not found:

<?php

require_once ('vendor/autoload.php');

$user = new \App\Vendas\Usuario();
$user->cadastrar('David', 30);

$p1 = new \App\Vendas\Produto();
$p2 = new \App\Vendas\Produto();

$p1->cadastrar(1, 'produto1');
$p2->cadastrar(2, 'produto2');

$c = new \App\Vendas\Compra();

$c->cadastrar( array('p1' => $p1, 'p2' => $p2), $user);

echo $c->imprimir();

$e = new \App\Estoque\Estoque();
echo $e->getTotal();
?>

My file Composer.json:

{
    "autoload":
    {
        "psr-4":{"App\\":"App/"}
    }
}

Directory structure:

App
---Vendas
    Compra.php
    Usuario.php
    Produto.php
---Estoque
    Produto.php
    Estoque.php
---vendor
composer.json
index.php
  • The directory vendor is inside App?

  • 1

    Try to change App/ for ./ on Composer and update (composer dump-autoload)

  • @Anderson Carlos Woss This inside App yes.

  • If you’re in App/vendor, in his index.php should be require_once('App/vendor/autoload.php'), otherwise you won’t be able to include the file. The question is, why vendor is in App?

  • And vendor should be where? The solution of @Guilherme Gostar worked, I wanted to understand why.

  • It’s all in App.

Show 1 more comment

1 answer

0


The problem is that the root folder name is not considered when defining "App\\": "App/" he will try to find the folder [...]/App/App the first being the project root folder and the second the namespace root folder App, the correct directory structure for your autoload would look something like this:

MeuProjetoIncrivel
---App
------Vendas
      Compra.php
      Usuario.php
      Produto.php
------Estoque
      Produto.php
      Estoque.php
---vendor
composer.json
index.php

Browser other questions tagged

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