Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

Publishing package assets the right way

Original – by Freek Van der Herten – 2 minute read

At Spatie we do not only create a lot of Laravel packages, but we use also use a bunch of existing ones. In this post I'd like to give a quick hint to our fellow package developers.

In the readme's of packages you'll often find an instruction like this to publish it's assets:

php artisan vendor:publish

This will not only publish the assets of that package, but also all unpublished assets of every package currently installed. Here's an example of the mess it can create. publish

To avoid this you should always add the provider-option pointing to your service provider. The publish command will then only publish the assets of your package. Here's an example taken from our backup package:

php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"

You can even use tags to create groups of assets. Here's an example taken from the readme of our laravel-googletagmanager package to only publish the config file (and not the view files).

php artisan vendor:publish --provider="Spatie\GoogleTagManager\GoogleTagManagerServiceProvider" --tag="config"

Such a group can be created by using the second parameter of the publishes-function in a serviceProvider. Here's the relevant code taken for the service provider of the aforementioned package.

$this->publishes([
    __DIR__.'/../resources/config/config.php' => config_path('googletagmanager.php'),
], 'config');

$this->publishes([
    __DIR__.'/../resources/views' => base_path('resources/views/vendor/googletagmanager'),
], 'views');

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "Publishing package assets the right way"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.