TerrePorter's avatar

add custom path

Hello, I’m using a modular setup to create a couple packages. I want to use livewire in those packages but it only looks in app\Livewire is there a way to add a additional path in my service provider to allow my package livewire files to be loaded? Thanks

0 likes
1 reply
LaryAI's avatar
Level 58

Certainly! To include Livewire components from a custom path, such as from within a package, you can modify the Livewire component discovery logic by specifying your custom path(s) in the boot method of your service provider. Here's how you can do it:

  1. Open your package's service provider or create one if it doesn't exist.
  2. In the boot method of your service provider, use the Livewire::component() method to manually register each component from your package.
  3. Alternatively, you can use the Livewire::componentNamespace() method to specify a namespace and path where Livewire should look for components.

Here's an example of how you might do this:

namespace Your\Package;

use Illuminate\Support\ServiceProvider;
use Livewire\Livewire;

class YourPackageServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Manually register components
        Livewire::component('package-component', \Your\Package\Http\Livewire\PackageComponent::class);

        // Or, set a custom namespace and directory for Livewire components
        $this->loadViewsFrom(__DIR__.'/path/to/views', 'your-package');
        Livewire::componentNamespace('Your\Package\Http\Livewire', 'your-package');
    }

    public function register()
    {
        //
    }
}

In the example above, replace Your\Package with the actual namespace of your package, and adjust the paths and names accordingly. The loadViewsFrom method is used to specify where your package's views are located, and the componentNamespace method tells Livewire to look for components within the specified namespace and path.

Remember to register your service provider in your package's composer.json file and in the config/app.php configuration file of the Laravel application if it's not auto-discovered.

After doing this, you should be able to use Livewire components from your package just like you would with regular app components.

Please or to participate in this conversation.