Simplifying Service Providers With Laravel Package Tools
Published on by Paul Redmond
Laravel Package Tools is a package by Spatie that provides an opinionated base service provider you can use to streamline the registration of your package’s config files, migrations, commands, and more.
Here’s how to simplify service providers in @laravelphp packageshttps://t.co/qKmIO7nUJY
Spoiler: I released a package for that https://t.co/URH2udPbiJ#php #laravel pic.twitter.com/tWCGuE8HG8
— Freek Van der Herten (@freekmurze) January 25, 2021
I think you’ll agree that this package can streamline common use-cases found in package service providers:
use Spatie\LaravelPackageTools\PackageServiceProvider;use Spatie\LaravelPackageTools\Package; class YourPackageServiceProvider extends PackageServiceProvider{ public function configurePackage(Package $package) : void { $package ->name('your-package-name') ->hasConfigFile() ->hasViews() ->hasTranslations() ->hasMigration('create_package_tables') ->hasCommand(YourCoolPackageCommand::class); }}
Spatie also provides a skeleton Laravel package you can use as a template for your next package project. The Laravel skeleton uses the package tools service provider out of the box.
Learn More
You can learn more about this package, get full installation instructions, and view the source code on GitHub. At the time of writing, the best place to get documentation is the project’s readme file.
Freek Van der Herten also wrote about Simplifying service providers in Laravel packages, which is an excellent deep-dive of why this package exists and how it can clean up your service providers.