Framework: Register anonymous components from a package

Created on 29 Mar 2020  路  13Comments  路  Source: laravel/framework


  • Laravel Version: 7.3.0

Description:

If I have a package that has some anonymous components, the only way I can make them available is to create a class for them and register them individually. There is no way to register anonymous components without writing a class for it.

For reference,
spatie/laravel-blade-x had BladeX::component('components.myAlert'); to register a component which is just a blade file.

Most helpful comment

I actually had to do this yesterday myself and it's easier than having to create a class for it.

First you'll have to place your components in a views directory in your package. Then you load those views in the boot method of your service provider:

$this->loadViewsFrom(__DIR__.'/../views', 'package-name');

Notice the "package-name" name there. You can now use these to register the components in your service provider:

Blade::component('package-name::your-component', 'your-component');

No class needed. I've been thinking of maybe doing some auto loading so the package-name:: notation works out of the box:

<x:package-name::your-component/>

Which could be cool. Anyway, that's a feature request and not a bug so closing this.

All 13 comments

+1

I actually had to do this yesterday myself and it's easier than having to create a class for it.

First you'll have to place your components in a views directory in your package. Then you load those views in the boot method of your service provider:

$this->loadViewsFrom(__DIR__.'/../views', 'package-name');

Notice the "package-name" name there. You can now use these to register the components in your service provider:

Blade::component('package-name::your-component', 'your-component');

No class needed. I've been thinking of maybe doing some auto loading so the package-name:: notation works out of the box:

<x:package-name::your-component/>

Which could be cool. Anyway, that's a feature request and not a bug so closing this.

@driesvints Thank you very much.
Yeah <x:package-name::your-component/> would be great, but your current solution is also good.

I think your solution can be added to the documentation. I found few more people were looking for this too.

@devfaysal feel free to send in a pr.

I actually had to do this yesterday myself and it's easier than having to create a class for it.

First you'll have to place your components in a views directory in your package. Then you load those views in the boot method of your service provider:

$this->loadViewsFrom(__DIR__.'/../views', 'package-name');

Notice the "package-name" name there. You can now use these to register the components in your service provider:

Blade::component('package-name::your-component', 'your-component');

No class needed. I've been thinking of maybe doing some auto loading so the package-name:: notation works out of the box:

<x:package-name::your-component/>

Which could be cool. Anyway, that's a feature request and not a bug so closing this.

@driesvints
Does not seem to be working for me. Did everything as described but keep getting "Unable to locate class or view" :-(

@gavinhewitt works fine for me. Share some code?

@driesvints Umm yeah, it works. Turns out it was an incomplete path that I somehow missed. Whoops! ;-)

Thanks!

@driesvints
i try

$this->loadViewsFrom(__DIR__.'/../Views', 'admin');
...
<x:admin::test/>

but not working
Unable to locate a class or view for component [admin::test]

working only

@component('admin::test')
@endcomponent

@Butochnikov you need to use Blade::component. See https://github.com/laravel/framework/issues/32154#issuecomment-605858282

@driesvints
But how to automatically register all the components from the package folder?

That's not possible atm.

https://github.com/laravel/framework/pull/33954 This PR will solve this issue in a nice way

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fuzzyma picture Fuzzyma  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

CupOfTea696 picture CupOfTea696  路  3Comments

kerbylav picture kerbylav  路  3Comments

iivanov2 picture iivanov2  路  3Comments