Creating Installer Commands for Laravel Packages

Admin   Laravel   702  2022-09-14 22:00:02

The Laravel Package Tools package by Spatie added a nifty feature we wanted to help share with the community: streamlined install commands for Laravel packages.

Typically, when installing a Laravel community package, the README will have instructions for publishing config files, migrations, etc. With Laravel Package Tools, you can now define a dedicated install command to automate all that:

1$package
2 ->name('your-package')
3 ->hasConfigFile()
4 ->hasInstallCommand(function(InstallCommand $command) {
5 $command
6 ->publishConfigFile()
7 ->publishMigrations()
8 ->askToRunMigrations()
9 ->copyAndRegisterServiceProviderInApp()
10 ->askToStarRepoOnGitHub();
11 });

Using a dedicated install command, your users only need to run one command instead of manually doing everyday installation tasks. Using the above package name, that would look like:

1php artisan your-package:install

The install command feature also includes startWith() and endWith() methods to add custom functionality to your install command.

Neat!

To get started with this package, check out spatie/laravel-package-tools on GitHub. Also, check out Creating installer commands for Laravel packages by the author Freek Van der Herten for background details of why this feature was added, including examples.

Source: laravel-news.com