Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to execute `composer dump-autoload` automatically after installing my package?

I wrote a Laravel package that I want to make available for everyone to download.

However, it seems that I have to manually execute composer dump-autoload after I add my package.

Is it possible to automatically execute composer dump-autoload after the install so that the composer will autoload all the new classes in my project?

like image 515
Jaylen Avatar asked Dec 28 '16 23:12

Jaylen


People also ask

What does dump-autoload do?

If you need to regenerate your package's autoload files, you may use the php artisan dump-autoload command. This command will regenerate the autoload files for your root project, as well as any workbenches you have created.

What is the difference between composer install and composer update?

composer install is primarily used in the 'deploying phase' to install our application on a production server or on a testing environment, using the same dependencies stored in the composer. lock file created by composer update.

How do I update composer by itself?

To update Composer itself to the latest version, run the self-update command. It will replace your composer.phar with the latest version. If Composer was not installed as a PHAR, this command is not available. (This is sometimes the case when Composer was installed by an operating system package manager.)


1 Answers

Yes, you can add it in the "post-update" script section of your composer.json. It'll then be executed after every update of the project. You can also add it inside the "post-install" section, so the command will be called directly after the installation.

A short excerpt from a composer.json:

"scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "composer dump-autoload", // Here.
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "composer dump-autoload" // Here too.
        ]
    },
like image 134
manniL Avatar answered Sep 21 '22 01:09

manniL