Does Poser autoload not work?

Asked

Viewed 2,964 times

2

I’m trying to use the autoload of composer and I can’t, he says the class not found and trying to load a class from an external library.

I’ve already executed on 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

  • In your code where it’s loaded vendor/autoload.php is not described in the code?

  • It is not clickable for the App namespaces?

  • it will only automatically load when on top of the script had require 'vendor/autoload.php'; then you can use understood ... ? or thing has to give a composer dump-autoload for it to create the entries in the upload file.

  • If the answer is useful accept it as a response?

1 answer

2

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

What a folder layout would look like:

inserir a descrição da imagem aqui

after making all your settings and downloading your package your file composer.json has the settings below:

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

and for these modifications to take effect, make the command:

php composer dump-autoload

ready. Now to use all code, including installed packages, you must put it at the top of the archive 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:

  • 1

    Thanks for the help.

  • 1

    @tiagopaes if it was useful accepted as an answer to your question!

Browser other questions tagged

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