Home > OS >  Custom Prefix to Livewire Components
Custom Prefix to Livewire Components

Time:08-24

I have created some LiveWire components in my Laravel Project and I want to add a custom prefix to these components.

Example.

I have a simple LiveWire component named Button created with php artisan make:livewire Button

It works fine when I use

<livewire:button />

but instead of livewire: prefix I want to use components: like this

<components:button />

I could not find any option for this.

Thanks in advance!!!

Best.

CodePudding user response:

Unfortunately, there doesn't appear to be an officialy supported way to change the prefix of the Livewire components.

If I haven't completely misunderstood the code you could "hack" it by extending the LivewireTagCompiler class and overwriting the compileLivewireSelfClosingTags method to use your own prefix. You'll then have to register it in any of your service providers' boot methods as the new precompiler of Blade.

$this->app['blade.compiler']->precompiler(function ($string) {
    return app(CustomLivewireTagCompiler::class)->compile($string);
});

It's a very ugly solution and could break if you are not careful and don't keep track of any changes made to the original class and I recommend just living with the livewire: prefix instead.

  • Related