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.
+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.
Thanks. Done https://github.com/laravel/docs/pull/5929
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
viewsdirectory 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
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
viewsdirectory in your package. Then you load those views in the boot method of your service provider:Notice the "package-name" name there. You can now use these to register the components in your service provider:
No class needed. I've been thinking of maybe doing some auto loading so the
package-name::notation works out of the box:Which could be cool. Anyway, that's a feature request and not a bug so closing this.