Laravel 11 Custom Component File Structure

Laravel 11 Custom Component File Structure

Would you like to create a component like this, or do you prefer to develop your own custom component in Laravel with a custom path?

Then you need to follow few steps

Step 1: Create a Service Provider

php artisan make:provider PackageServiceProvider

You can name it your own.

Step 2: Add Blade::componentNamespace(‘AppViewBackendComponents’, ‘backend’); on your boot() method

<?php

namespace AppProviders;

use IlluminateSupportFacadesBlade;
use IlluminateSupportServiceProvider;

class PackageServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
//
}

/**
* Bootstrap services.
*/
public function boot(): void
{
Blade::componentNamespace(‘AppViewBackendComponents’, ‘backend’);
}
}

Step 3: Create a php file as per your file structure. I created my file here.

appViewBackendComponentsAdminSidebar.php

Step 4: Return your blade directive on render() method.

<?php

namespace AppViewBackendComponents;

use IlluminateViewComponent;
use IlluminateViewView;

class AdminSidebar extends Component
{
/**
* Get the view / contents that represents the component.
*/
public function render(): View
{
return view(‘backend.components.sidebar’);
}
}

Step 5: Create your blade file as you mentioned. For my case I added like this.

resourcesviewsbackendcomponentssidebar.blade.php

Step 6: Your Component setup is done. now you can access that component by this-

<x-backend::admin-sidebar />

Hope this will help you. Thank You.