Unless you have a app/View/Components/Card/Card.php class along with that View, then it is considered an anonymous component which from what I can tell doesn’t support “Index Components” from your docs link.
EDIT: Whoops, actually it does, just a different structure putting card.blade.php outside of the card directory. See the section right below the section I linked above: Anonymous Index Components
Thankfully, Blade allows you to place a file matching the component’s directory name within the component’s directory itself. When this template exists, it can be rendered as the “root” element of the component even though it is nested within a directory. So, we can continue to use the same Blade syntax given in the example above; however, we will adjust our directory structure like so:
I also tried to add the app/View/Components/Card/Card.php class with command acorn make:component Card/Card:
<?php
namespace App\View\Components\Card;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
class Card extends Component
{
/**
* Create a new component instance.
*/
public function __construct()
{
//
}
/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
return view('components.card.card');
}
}
but again, doesn’t work, same error: # Unable to locate a class or view for component [card]
Maybe I need some other actions? Launching some commands (clear cache) or something?
So you moved resources/views/components/card/card.blade.php to resources/views/components/card.blade.php like suggested in my edited docs link above?
I think creating a component class just for this is pretty overkill and I’d definitely recommend simply doing the above, but since you’re flipping back and forth between component types, you might need to run wp acorn optimize:clear for the View component class to get discovered. If you closely follow the Laravel docs, this should all otherwise work as expected.
Moving card.blade.php to /components works, it’s the default, it always worked.
I would have liked to put it under /components/card to better organize that component.
Anyway it’s not a big issue, but it’s strange that it doesn’t work since the Laravel docs says it should work under /components/card …
About the class I’m with you, I’d like to avoid it if possible, since all my components are just views (hope this is a right way to use them).
About the class I’m with you, I’d like to avoid it if possible, since all my components are just views (hope this is a right way to use them).
As per the Laravel docs, you have to have the class to organize it how you want. If you don’t have a class for the view component, the only option is to keep the view in the root component directory like resources/views/components/card.blade.php.
Just to clarify, there’s 2 different sections on the Laravel docs for this.
That being said, the behavior you’re seeing is expected as per the documentation. If you try creating a component with a class again and run into an error, try running wp acorn optimize:clear and/or composer dump-autoload.
Maybe I’m wrong but this is from the docs for Anonymous Index Components
Thankfully, Blade allows you to place a file matching the component’s directory name within the component’s directory itself. When this template exists, it can be rendered as the “root” element of the component even though it is nested within a directory. So, we can continue to use the same Blade syntax given in the example above; however, we will adjust our directory structure like so: /resources/views/components/accordion/accordion.blade.php /resources/views/components/accordion/item.blade.php
Isn’t it saying that I can put accordion.blade.php in the subfolder /components/accordion and including with <x-accordion> and not <x-accordion.accordion>?
That being said, the behavior you’re seeing is expected as per the documentation. If you try creating a component with a class again and run into an error, try running wp acorn optimize:clear and/or composer dump-autoload .
I tried again with the class (code posted above); the view is in components/card/card.blade.php and included as <x-card>; launched those 2 commands. Same error as before.
You’re right! I was completely thrown off by how that section started and didn’t realize under the initial explanation they said that the original folder-based method should work as well.
I went ahead and gave it a test in my project creating resources/views/components/test/test.blade.php and was able to call <x-test /> without any issue.
Are you on the latest version of Acorn (5.x)? If not, I suspect that might be the culprit with this functionality having been originally added in Laravel 11.x.