0

For some reason, I put my Laravel blade components inside the App/Core/CoreComponents folder, so now I can't use these components like the old way:

<x-Core.CoreComponents.input />

And after searching I found a way by using this in AppServiceProvidor.php to make alias

// AppServiceProvidor.php in boot()
Blade::componentNamespace('CoreComponents', 'Core.CoreCoreComponents');

// use the components in blade.php
<x-CoreComponents::input />

but it does not work, so is there any way to do that? and does it affect the performance? (note that I really want to put them inside App folder)

2 Answers 2

0

Follow the document at https://laravel.com/docs/master/blade#manually-registering-components, I think you have a typo:

<x-Core.CoreComponents::input />

instead of

<x-Core.CoreComponents.input />

And when register components namepsace, you have to use full namespace:

Blade::componentNamespace('App\\Core\\CoreComponents', 'Core.CoreCoreComponents');
0

my web app does its job from the app/view. Anyways, I just read about the same topic on laravel docs:

components are automatically discovered within the app/View/Components directory and resources/views/components directory bu you can do this to your package's service provider:

use Illuminate\Support\Facades\Blade;
use App\Core\CoreComponents\InputComponent; //example

public function boot(): void
{
    Blade::component('input', InputComponent::class);    
//OR
    Blade::anonymousComponentPath(__DIR__.'/../Core/CoreComponents');
//OR the one you discovered already:
    Blade::componentNamespace('App\\Core\\CoreComponents', 'Core.CoreComponents'); //hint: <x-Core.CoreComponents::input>
}
1
  • it still not working and show this exception: "Unable to locate a class or view for component [Core.CoreComponents::input].". i tried to clear cache and views and composer dump-autoload. and these what i used: // this in create.blade.php <x-Core.CoreComponents::input cols="6" type="text" id="name_en" name="name_en" :model="$model" /> // and this in boot(): Blade::componentNamespace('App\\Core\\CoreComponents', 'Core.CoreComponents'); Commented Oct 3, 2023 at 4:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.