4

I'm working on a simple project using PHP and I'm using PSR-4 autoloading. I just wonder if there's a way to run composer dump-autoload -o on composer install, so that new users won't need to type composer dump-autoload -o to avoid autoloading error

{
    "name": "project/vendor",
    "license": "MIT",
    "authors": [
        {
            "name": "my name",
            "email": "myemail@gmail.com"
        }
    ],
    "require": {
        "phpunit/phpunit": "^7.5"
    },
    "autoload": {
          "psr-4": {
              "src\\": "src/"
           }
     },
     "scripts": {
        "run-test": ["./vendor/bin/phpunit tests/calculatorTests.php"]
    }
}
rob006
  • 21,383
  • 5
  • 53
  • 74
21 Century
  • 43
  • 1
  • 6
  • dump-autoload -o is only to create an optimized autoload map for use in production. For development you don't need it, and just a composer install should suffice. – rpkamp Jan 05 '19 at 19:41
  • Actually after testing the code on another machine, i executed composer install but after i got an error which was a class is not found, and it worked just fine after executing composer dump-autoload – 21 Century Jan 05 '19 at 19:43
  • That should not happen. Could you update your post to include your full composer.json? – rpkamp Jan 05 '19 at 19:44
  • okay i updated the post and here's the link of the screenshot: https://i.stack.imgur.com/2oMT7.png – 21 Century Jan 05 '19 at 19:47
  • Please do not post images of plaintext. [Images of plaintext are not appropriate on StackOverflow](https://meta.stackoverflow.com/a/285557/3784008). You have access to the plaintext; please copy and paste it into your question. – anothermh Jan 05 '19 at 22:43
  • 1
    @anothermh i did thanks – 21 Century Jan 05 '19 at 22:48

1 Answers1

10

You may use optimize-autoloader option in composer.json config:

"config": {
    "optimize-autoloader": true
},

However this may be quite annoying on development phase (when generating optimized autoloader is just waste of time and source of problems), so I would rather create some shortcut script for this and encourage users to use it instead of direct composer dump-autoload or composer install. See this answer as an example. Then you can just use

composer install --no-dev -o 

in your script.

rob006
  • 21,383
  • 5
  • 53
  • 74