Search code examples
phplaravelcomposer-phplaravel-8laravel-artisan

Laravel auto-publish the package configs to the project


I have some config files in my package that wanted to be copied over to the project or app config directory. I know how to publish it manually by running the command php artisan vendor:publish. Anyone here knows how to publish them automatically? Like I don't have to remember that I have some config files in my packages when I install the app in production?


Solution

  • Found the answer in composer.json of the project. Just need to add a command in the scripts section, under post-autoload-dump:

      "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi",
            "@php artisan vendor:publish --tag={tagNameHere}--force"
         ]
       },
    

    I defined the tag at my package ServiceProvider.php:

      public function boot()
      {
        $this->publishes([
            //file source => file destination below
            __DIR__.'/config/someconfig.php' => config_path('someconfig.php'),
           //you can also add more configs here
          ],
          ['tagNameHere'] //this is the tag so you can run the command as php artisan vendor:publish --force --tag=tagNameHere, for more info run php artisan vendor:publish --help
        );
      }
    

    Every time I run composer update or composer dump-autoload, the configs of my dependency is automatically copied over so I don't have to remember that I have something to publish.