PHPackages                             theriddleofenigma/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. theriddleofenigma/laravel-package-tools

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

theriddleofenigma/laravel-package-tools
=======================================

Tools for creating Laravel packages

v0.1.2(4y ago)010MITPHPPHP ^7.4|^8.0

Since Feb 25Pushed 4y ago1 watchersCompare

[ Source](https://github.com/theriddleofenigma/laravel-package-tools)[ Packagist](https://packagist.org/packages/theriddleofenigma/laravel-package-tools)[ Docs](https://github.com/theriddleofenigma/laravel-package-tools)[ RSS](/packages/theriddleofenigma-laravel-package-tools/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (3)Dependencies (5)Versions (4)Used By (0)

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

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

[![Latest Version on Packagist](https://camo.githubusercontent.com/a017a438a13f4c95cc4f29c322c94305a4886909794f380859abe69b96d6cdb8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746865726964646c656f66656e69676d612f6c61726176656c2d7061636b6167652d746f6f6c732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/theriddleofenigma/laravel-package-tools)[![Tests](https://github.com/theriddleofenigma/laravel-package-tools/workflows/Tests/badge.svg)](https://github.com/theriddleofenigma/laravel-package-tools/workflows/Tests/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/f7b968b02d34fb9764d575632b6550d571798d1b63ad831c3727c4e3d3561ae6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746865726964646c656f66656e69676d612f6c61726176656c2d7061636b6167652d746f6f6c732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/theriddleofenigma/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 Enigma\LaravelPackageTools\PackageServiceProvider;
use Enigma\LaravelPackageTools\Package;
use MyPackage\ViewComponents\Alert;

class YourPackageServiceProvider extends PackageServiceProvider
{
    public function configurePackage(Package $package): void
    {
        $package
            ->name('your-package-name')
            ->hasConfigFile()
            ->hasViews()
            ->hasViewComponent('enigma', 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.

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/theriddleofenigma/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 `Enigma\LaravelPackageTools\PackageServiceProvider`.

```
use Enigma\LaravelPackageTools\PackageServiceProvider;
use Enigma\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', 'Enigma');
```

### 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('enigma', Alert::class);
```

This will register your view components with Laravel. In the case of `Alert::class`, it can be referenced in views as ``, where `enigma` 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`...

```
