Structure Scout
WireUse - Advanced - 2024-07-29
Description
If you have a lot of Blade or Livewire components, it may take a lot of work to register them manually, especially in multi-tenancy applications.
Our component scout can help with this. It is supported by spatie/php-structure-discoverer package, which also offers benefits such as caching.
We recommend using a domain-driven-design (DDD) pattern, such as src/App/Posts/Components/Card.php
.
Spatie offers an excellent in dept course for this: https://spatie.be/products/laravel-beyond-crud
Installation
Structure Scouts are disabled by default, and can be enabled in config/wireuse.php
:
php artisan vendor:publish --tag="wireuse-config"
'scout' => [
'enabled' => true,
'cache_store' => null,
'cache_lifetime' => 60 * 60 * 24 * 7,
],
Usage
Blade Components
To discover Blade components, create and register a service provider:
php artisan make:provider ViewServiceProvider
use Foxws\WireUse\Scout\ComponentScout;
use Illuminate\Support\ServiceProvider;
class ViewServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->registerComponents();
}
protected function registerComponents(): static
{
ComponentScout::create(app_path('Web'), 'App\\')->register();
return $this;
}
}
To call a registered Blade component:
<x-web.posts.card :$item />
Livewire Components
To discover Livewire components, create and register a service provider:
php artisan make:provider LivewireServiceProvider
use Foxws\WireUse\Scout\LivewireScout;
use Illuminate\Support\ServiceProvider;
class LivewireServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->registerComponents();
}
protected function registerComponents(): static
{
LivewireScout::create(app_path('Web'), 'App\\')->register();
return $this;
}
}
To call a registered Livewire component:
<livewire:web.posts.tags />