Composer Autoload does not work?

2

I'm trying to use autoload of composer and I can not, it says that the not found class is trying to load a class from an external library.

I've already run the command line:

composer install 

and

composer dump

unsuccessful.

Composer.json

"autoload": {
    "psr-4":
    {
        "App\":
        [
            "app/",
            "tests/"
        ]
    }
}

Code

<?php

  namespace App\Database;
  use Dotenv\Dotenv;

  $dotenv = new Dotenv('../../');
  $dotenv->load();


  class Database
  {...

Error

  

Fatal error: Uncaught Error: Class 'Dotenv\Dotenv' not found in /home/vagrant/Projetos/qa-toll/src/Database/Database.php on line 7

    
asked by anonymous 15.06.2017 / 17:39

1 answer

1

The autoload of composer.phar works as follows when installing packages and or configuring some class there is a file that is required to make its declaration at the top of script so that it has access to all code produced by the developer or downloaded by composer .

What would a folder layout look like:

  

Afterdoingallyoursettingsanddownloadingyourpackageyourfilecomposer.jsonhasthefollowingsettings:

{"require": {
        "vlucas/phpdotenv": "^2.4"
    },
    "autoload": {
        "psr-4": {
            "App\": [
                "app/",
                "tests/"
            ]
        }
    }
}

and for these changes to take effect, issue the command:

php composer dump-autoload

Ready. Now to use all the code, including installed packages, you should put it at the top of the file and include require vendor/autoload.php , example :

index.php

<?php

    // aqui que define todo o carregamento e disponibilidade do código
    require_once 'vendor/autoload.php';

    use Dotenv\Dotenv;
    use App\Database;

    $dotEnv = new Dotenv(__DIR__);
    $dotEnv->load();

    $database = new Database();
    echo $database->getConfigServer();

Good reading:

16.06.2017 / 17:24