Simplifying Service Providers With Laravel Package Tools

Packages

January 26th, 2021

laravel-package-tools-featured.png

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:

1use Spatie\LaravelPackageTools\PackageServiceProvider;
2use Spatie\LaravelPackageTools\Package;
3 
4class YourPackageServiceProvider extends PackageServiceProvider
5{
6 public function configurePackage(Package $package) : void
7 {
8 $package
9 ->name('your-package-name')
10 ->hasConfigFile()
11 ->hasViews()
12 ->hasTranslations()
13 ->hasMigration('create_package_tables')
14 ->hasCommand(YourCoolPackageCommand::class);
15 }
16}

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.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.