Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer classmap autoload does not load new files in folder

Tags:

The following problem: I have defined a classmap in my composer.json:

"autoload": {     "classmap": [         "app/controllers",         "app/models",         "app/helper.php"     ]    } 

However, when I create a new file in the "controllers" or "models" folder, it will not load them and I always have to make a composer dump-autoload.

Is this the correct behavior? I thought the autoloader from composer monitors the folder for new files then?

like image 873
pyxl Avatar asked Sep 02 '14 08:09

pyxl


2 Answers

Yes, this is correct behaviour. If you want new classes to be loaded automatically, you have to use either PSR-0 or PSR-4 autoloading.

Generating the classmap requires Composer to know the filename that contains a certain class. This can only be done by parsing the whole source code in the directory and scanning for classes, interfaces and trait definitions.

This usually is a CPU and I/O intensive task, so it is only done when Composer does install/update or (on demand) dumps the autoloader, it is not done with every require "vendor/autoload.php";.

Note that the classmap autoloading is simply there for old legacy codebases that didn't implement at least PSR-0. It is not intended for new code - unless you want to pay the price to dump the autoloader again and again during development.

like image 118
Sven Avatar answered Sep 28 '22 06:09

Sven


Go to the root of your server by SSH. Now do the following:

  1. Run ls to list all the files.
  2. You will see composer.lock file; remove the file with rm composer.lock command.
  3. Now run php composer update command.

Depending on your linux type you may have to run php-cli composer update.

Step 3 will create a new composer.lock file and all your classes will be loaded again. Do this anytime you add new classes.

or:

  1. Run composer dump-autoload command.
like image 28
Samuel Kwame Antwi Avatar answered Sep 28 '22 04:09

Samuel Kwame Antwi