skip to Main Content

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


  1. 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');
    
    Login or Signup to reply.
  2. 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 IlluminateSupportFacadesBlade;
    use AppCoreCoreComponentsInputComponent; //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>
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search