How To Integrate Livewire in Package?

I have a package that I would like to convert from JavaScript/Vue to Livewire. My goal is to have the package manage all the Livewire components without the user having to take extra action: I would like to set up the entire Livewire ecosystem for my package in my package’s service provider. Is this possible?

I’ll take a stab since nobody else has. I think everything would be fine as long as all your name spacing and package discovery is in order. You might run into problems, or just need extra configuration if you’re going to allow the livewire components to be published.

But I could be wrong, this is just an educated guess. I have a basic understanding of packages and laravel specific packages, but I’ve never actually done one myself.

Hi @xxdalexx, thanks for taking the time to respond. I have gotten almost everything figured out, with exception of the detection of Livewire Component classes due to the class_namespace config setting being set to the main app namespace (which shouldn’t be changed). For now I am working around this by publishing the component classes to the main app, but preferably this wouldn’t be necessary.

@calebporzio would it be possible to add multiple namespaces to the configuration setting, so that the Livewire classes could live inside my package, and not interfere with the developer’s Livewire classes in the main app?

Hey @mikebronner, you can register components in a service provider like so:

Livewire::component('your-package::your-component', YourComponent::class);

Does that solve your problem?

2 Likes

Thanks @calebporzio, I’ll give that a shot and report back. :slight_smile:

Update: worked like a treat! brilliant. :slight_smile:

I’m trying to rebuild a package with Livewire too. However my package view just shows @livewireScripts and @livewireStyles on the page as if Livewire’s not there. I’ve added it as a dependancy of my package and I’m testing with a local path repository at the moment. Should this work, am I missing something obvious?

Have you registered your components in your service provider as Caleb suggested?

I’ve not got as far as my own components — just including Livewire and the @livewireScripts directives are ignored :thinking:

this is another issue.
use @livewireScripts() instead. :wink:

I was excited that might solve the problem – but now I just see @livewireScripts() in the view instead!

Should Livewire be auto discovered? When I require my package in my demo install I see other packages auto discovered that are required but not Livewire. This may be more to do with my configuration than a Livewire problem. I’m probably doing something stupid!

If your package is auto discovered via “extra/laravel/providers” in your composer.json then you need to add manual registration of your components into service provider boot section like Caleb mentioned above. Like:

public function boot()
     {
        if (!$this->app->runningInConsole()) {
\Livewire\Livewire::component('flatform::table', \dimonka2\flatform\Livewire\TableComponent::class);

I guess the rest (publishing views etc) should be no problem for you.

1 Like

Thank you for taking the time to help @Dimonka2. My package currently provides a Vue app to administer the packages functionality — I’m hoping to change it to a Livewire app. First step was to install Livewire as a decency for my package but the route that used to show my Vue app now just shows

@livewireStyles()
Hello World
@livewireScripts()

It doesn’t seem to register Livewire at all. I’m sure I must be missing something, but I don’t have any components to register yet. Would having a component kick Laravel into seeing Livewire?

My guess is that you pasting your blade directives (@livewireStyles) somewhere outside of a blade.
Otherwise it should work.

So looks like the way my package was included was causing the problem. Somehow my package was being symlinked but composer wasn’t installing it’s new dependancies — I’ve managed to get composer to reinstall my local package and it’s installed livewire decency, so it’s working now. Thanks for you time @Dimonka2 :+1:

i still got this error

Livewire\Exceptions\ComponentNotFoundException

Component [product-status] class not found: [Skylarnet\SHOP\Livewire\productStatus]

but in mine valet instance its working? and livewire:discover wont work

and in mine SHopServiceProvider did load it in boot()

\Livewire\Livewire::component(‘product-status’, \Skylarnet\SHOP\Livewire\productStatus::class);