PHPackages                             digiservnet/laravel-package-tools - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. digiservnet/laravel-package-tools

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

digiservnet/laravel-package-tools
=================================

Tools for creating Laravel packages

1.12.0(4y ago)010MITPHPPHP ^7.3|^7.4|^8.0

Since Jan 24Pushed 4y agoCompare

[ Source](https://github.com/digiservnet/laravel-package-tools)[ Packagist](https://packagist.org/packages/digiservnet/laravel-package-tools)[ Docs](https://github.com/spatie/laravel-package-tools)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/digiservnet-laravel-package-tools/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (5)Versions (33)Used By (0)

Tools for creating Laravel packages
===================================

[](#tools-for-creating-laravel-packages)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ca6b11789d1a89361b7434a51aa207df98a92552fdea6d41bcfa50f6c9489c50/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d7061636b6167652d746f6f6c732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-package-tools)[![Tests](https://github.com/spatie/laravel-package-tools/workflows/Tests/badge.svg)](https://github.com/spatie/laravel-package-tools/workflows/Tests/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/622107fb2f3556fbb0b98fdda87867a05f234ced0a54d4c5dd525155ce4cfea0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d7061636b6167652d746f6f6c732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-package-tools)

This package contains a `PackageServiceProvider` that you can use in your packages to easily register config files, migrations, and more.

Here's an example of how it can be used.

```
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Spatie\LaravelPackageTools\Package;
use MyPackage\ViewComponents\Alert;

class YourPackageServiceProvider extends PackageServiceProvider
{
    public function configurePackage(Package $package): void
    {
        $package
            ->name('your-package-name')
            ->hasConfigFile()
            ->hasViews()
            ->hasViewComponent('spatie', Alert::class)
            ->hasViewComposer('*', MyViewComposer::class)
            ->sharesDataWithAllViews('downloads', 3)
            ->hasTranslations()
            ->hasAssets()
            ->hasRoute('web')
            ->hasMigration('create_package_tables')
            ->hasCommand(YourCoolPackageCommand::class);
    }
}
```

Under the hood it will do the necessary work to register the necessary things and make all sorts of files publishable.

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/362cb51f7a4c462d27c8445dcab53e98821404ada8d5b265e68299381cbc13c2/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d7061636b6167652d746f6f6c732e6a70673f743d31)](https://spatie.be/github-ad-click/laravel-package-tools)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Getting started
---------------

[](#getting-started)

This package is opinionated on how you should structure your package. To get started easily, consider using [our package-skeleton repo](https://github.com/spatie/package-skeleton-laravel) to start your package. The skeleton is structured perfectly to work perfectly with the `PackageServiceProvider` in this package.

Usage
-----

[](#usage)

In your package you should let your service provider extend `Spatie\LaravelPackageTools\PackageServiceProvider`.

```
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Spatie\LaravelPackageTools\Package;

class YourPackageServiceProvider extends PackageServiceProvider
{
    public function configurePackage(Package $package) : void
    {
        $package->name('your-package-name');
    }
}
```

Passing the package name to `name` is mandatory.

### Working with a config file

[](#working-with-a-config-file)

To register a config file, you should create a php file with your package name in the `config` directory of your package. In this example it should be at `/config/your-package-name.php`.

If your package name starts with `laravel-`, we expect that your config file does not contain that prefix. So if your package name is `laravel-cool-package`, the config file should be named `cool-package.php`.

To register that config file, call `hasConfigFile()` on `$package` in the `configurePackage` method.

```
$package
    ->name('your-package-name')
    ->hasConfigFile();
```

The `hasConfigFile` method will also make the config file publishable. Users of your package will be able to publish the config file with this command.

```
php artisan vendor:publish --tag=your-package-name-config
```

Should your package have multiple config files, you can pass their names as an array to `hasConfigFile`

```
$package
    ->name('your-package-name')
    ->hasConfigFile(['my-config-file', 'another-config-file']);
```

### Working with views

[](#working-with-views)

Any views your package provides, should be placed in the `/resources/views` directory.

You can register these views with the `hasViews` command.

```
$package
    ->name('your-package-name')
    ->hasViews();
```

This will register your views with Laravel.

If you have a view `/resources/views/myView.blade.php`, you can use it like this: `view('your-package-name::myView')`. Of course, you can also use subdirectories to organise your views. A view located at `/resources/views/subdirectory/myOtherView.blade.php` can be used with `view('your-package-name::subdirectory.myOtherView')`.

#### Using a custom view namespace

[](#using-a-custom-view-namespace)

You can pass a custom view namespace to the `hasViews` method.

```
$package
    ->name('your-package-name')
    ->hasViews('custom-view-namespace');
```

You can now use the views of the package like this:

```
view('custom-view-namespace::myView');
```

#### Publishing the views

[](#publishing-the-views)

Calling `hasViews` will also make views publishable. Users of your package will be able to publish the views with this command:

```
php artisan vendor:publish --tag=your-package-name-views
```

### Sharing global data with views

[](#sharing-global-data-with-views)

You can share data with all views using the `sharesDataWithAllViews` method. This will make the shared variable available to all views.

```
$package
    ->name('your-package-name')
    ->sharesDataWithAllViews('companyName', 'Spatie');
```

### Working with Blade view components

[](#working-with-blade-view-components)

Any Blade view components that your package provides should be placed in the `/src/Components` directory.

You can register these views with the `hasViewComponents` command.

```
$package
    ->name('your-package-name')
    ->hasViewComponents('spatie', Alert::class);
```

This will register your view components with Laravel. In the case of `Alert::class`, it can be referenced in views as ``, where `spatie` is the prefix you provided during registration.

Calling `hasViewComponents` will also make view components publishable, and will be published to `app/Views/Components/vendor/`.

Users of your package will be able to publish the view components with this command:

```
php artisan vendor:publish --tag=your-package-name-components
```

### Working with view composers

[](#working-with-view-composers)

You can register any view composers that your project uses with the `hasViewComposers` method. You may also register a callback that receives a `$view` argument instead of a classname.

To register a view composer with all views, use an asterisk as the view name `'*'`.

```
$package
    ->name('your-package-name')
    ->hasViewComposer('viewName', MyViewComposer::class)
    ->hasViewComposer('*', function($view) {
        $view->with('sharedVariable', 123);
    });
```

### Working with translations

[](#working-with-translations)

Any translations your package provides, should be placed in the `/resources/lang/` directory.

You can register these translations with the `hasTranslations` command.

```
$package
    ->name('your-package-name')
    ->hasTranslations();
```

This will register the translations with Laravel.

Assuming you save this translation file at `/resources/lang/en/translations.php`...

```
