Composer is a major part of the Laravel MVC Framework, but it also exists without Laravel. In fact you could use it in any project. This article digs into the different files that are used by composer. It’s important to understand what these files are and what these files do.

composer.json

This is the only file you have to edit manually. In this file you can lists which packages you want and which versions of that package you want to install. Versions can be vague (1.x.x) or specific (1.1.2).

vendor/composer/autoload_classmap.php

  • This file (no class) returns an array of all aliasses and files based on the autoload section in composer.json.
  • This file is regenerated on each dump-autoload. If you have a new class somewhere in your project it will not be loaded unless it is included in autoload_classmap (hence you have to execute composer dump-autoload)
  • In Laravel, composer.json includes all controllers, models, commands, migrations, seeds, services and facades in your root folder structure. If you want a custom folder to dump files, you have to add it to the autoload-section in composer.json. That way it will be included in the autoload_classmap.php
  • autoload_classmap.php also includes the providers in config/app.php
  • In Laravel, the autoload_classmap is included inside app/bootstrap/autoload.php (as /../vendor/autoload.php which includes the autoload_classmap)

composer.lock

  • This file is not, as it might suggest, an indication of an update of an install going on. It’s not.
  • composer.lock lists all exact versions of each vendor package that is installed.
  • If you run composer install and there is a lock file present, it will download the versions of composer.lock no matter what’s inside composer.json
  • If you run composer install and there is no lock file, it will generate a lock file of all the vendor versions it has installed based on composer.json
  • If you run composer update it will overwrite the composer.lock file with the newest available vendor packages based on composer.json
  • This means that if you include composer.lock in your GIT repository; clone and execute composer install on another computer, it will download the exact same versions as in composer.lock

What’s the difference between composer dump-autoload, composer update and composer install?

The above text already explains the difference between those commands, but for fast readers:

  • composer install installs the vendor packages according to composer.lock (or creates composer.lock if not present),
  • composer update always regenerates composer.lock and installs the lastest versions of available packages based on composer.json
  • composer dump-autoload won’t download a thing. It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php). Ideal for when you have a new class inside your project.
    • Ideally, you execute composer dump-autoload -o , for a faster load of your webpages. The only reason it is not default, is because it takes a bit longer to generate (but is only slightly noticable)

Rss Comments

20 comments

  1. Very helpful article, thanks Robin.

    This has helped me understand a bit more what goes on behind the scenes with Composer, in particular how it automatically updates vendor/composer/autoload_classmap.php every time composer deump-autoload is run.

    #1 Jono
  2. It is very helpful,
    Thank you!

    #2 ThuongLN
  3. Very useful, thanks for posting.

    #3 Craig R Morton
  4. Thanks mate!

    #4 Alex
  5. Thank you!

    #5 Marco
  6. Nice article. Short and very useful. Thanks!

    #6 Jato
  7. Thanks for information. It was very useful.

    #7 Ganesh
  8. Thanks for the clarity…

    #8 ben
  9. Possibly the best and shortest explanation
    of composer core commands

    #9 Bruno
  10. #10 composer dump-autoload in Laravel – CastlePress
  11. Brief and clear post! Thanks a lot!

    #11 Konstantin
  12. Tnq s , great post

    #12 majid
  13. Thank you so much Robin! Nice Info.

    #13 Rajni
  14. hai, i want to ask.
    if i have the project with composer and it’s autoloader and i want to move the project to another PC. is there any configuration beside install the composer in the OS ?

    because when i run the project in localhost (another PC). i think the composer autoloder didn’t work.

    there is error like this
    PHP fata error : class ‘fMessaging’ not found in ….

    but in the old PC, the source code run smoothly without fatal error like that (same source code).

    Thanks.

    #14 iqbal
  15. still helpfull in 2017 ! thank you 😉

    #15 ben
  16. Great! Helped me a lot. Thanks!

    #16 Menno
  17. thanks men, this really clear things up to me. very simple and direct to the point. i was just actually looking for “composer dump-autoload”, yet i also got more than what im looking for.

    #17 Kapitan
  18. Thanks!!

    #18 jaskaran
  19. Very useful thanks for sharing.

    #19 Mahaveer
  20. Thank you

    #20 jiramet

Leave a comment






Wordpress.org clearPaper by CreativeBits.it Copyright © 2012-2019 Robin Brackez. All rights reserved. By visiting this site you agree to accept cookies that are purely used to check how many visitors I have. Theme by: creativebits. Custom adaptations by Robin Brackez.