Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Additional anonymous component paths in Laravel 9.x

Anonymous components are a great way to create reusable components in Laravel. Essentially, you can create an anonymous component without creating an associated class. For example, you can do something like this.

<x-alert>
    <x-slot name="title">
        Error
    </x-slot>

    <x-slot name="message">
        Something went wrong.
    </x-slot>
</x-alert>

As you can tell, the above code will render the alert component. However, you don’t have to create a class for the alert component. You can simply create an alert.blade.php file in the resources/views/components directory and use it as an anonymous component.

Also, notice it’s important that you put all your anonymous components in the resources/views/components directory. But sometimes, you may want to put your anonymous components in a different directory. For example, you may want to put all your anonymous components in the resources/views/anonymous-components directory.

That’s where the newly added anonymousComponentPath method comes into play.

The anonymousComponentPath method

The anonymousComponentPath method is a new method added to Laravel 9.x that can be accessed through the Illuminate\Support\Facades\Blade facade. This method allows you to register additional anonymous component paths apart from the resources/views/components directory.

For example, you can register the resources/views/anonymous-components directory as an additional anonymous component path in the AppServiceProvider’s boot method like so.

use Illuminate\Support\Facades\Blade;

public function boot()
{
    Blade::anonymousComponentPath(__DIR__.'/../anonymous-components');
}

You can register as many additional anonymous component paths as you want. This can be useful when you want to organize your anonymous components in their respective directories based on their purpose.

Namespacing anonymous components

Additionally, you can also specify a namespace for the anonymous component path by passing it as the second argument to the anonymousComponentPath method like so.

use Illuminate\Support\Facades\Blade;

public function boot()
{
    Blade::anonymousComponentPath(
        __DIR__.'/../payment-components',
        'payment'
    );
}

And then, you can use the payment namespace to access the anonymous components in the resources/views/payment-components directory like so.

<x-payment:alert>
    <x-slot name="title">
        Error
    </x-slot>

    <x-slot name="message">
        Something went wrong.
    </x-slot>
</x-payment:alert>
Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?