Generating optimized autoloader automatically
Asked Answered
S

1

5

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": "[email protected]"
        }
    ],
    "require": {
        "phpunit/phpunit": "^7.5"
    },
    "autoload": {
          "psr-4": {
              "src\\": "src/"
           }
     },
     "scripts": {
        "run-test": ["./vendor/bin/phpunit tests/calculatorTests.php"]
    }
}
Silver answered 5/1, 2019 at 19:30 Comment(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.Indomitability
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-autoloadSilver
That should not happen. Could you update your post to include your full composer.json?Indomitability
okay i updated the post and here's the link of the screenshot: i.sstatic.net/2oMT7.pngSilver
Please do not post images of plaintext. Images of plaintext are not appropriate on StackOverflow. You have access to the plaintext; please copy and paste it into your question.Unbraid
@Unbraid i did thanksSilver
H
12

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.

Haun answered 6/1, 2019 at 10:9 Comment(1)
Thanks, that's what i was looking forSilver

© 2022 - 2024 — McMap. All rights reserved.