spatie/laravel-view-components
Abandoned package. Provides a “view components” pattern for Laravel: classes implementing Htmlable that encapsulate view-related logic and render HTML (often via Blade). Use @render to pass data, and wrap third‑party HTML builders like menus.
Installation:
composer require spatie/laravel-view-components
(Note: Despite being archived, the package remains functional for Laravel 5.6+.)
First Component:
Create a class implementing Illuminate\Contracts\Support\Htmlable (e.g., app/Http/ViewComponents/NavigationComponent.php):
namespace App\Http\ViewComponents;
use Illuminate\Http\Request;
use Illuminate\Contracts\Support\Htmlable;
class NavigationComponent implements Htmlable
{
public function __construct(public Request $request) {}
public function toHtml(): string
{
return '<nav>...</nav>';
}
}
Register in Blade:
Add to config/app.php under view > components:
'components' => [
'navigation' => App\Http\ViewComponents\NavigationComponent::class,
],
Use in Blade:
@component('navigation')
@endcomponent
Replace a static navigation partial with a dynamic component:
// Component logic fetches active routes, user permissions, etc.
public function toHtml(): string
{
$active = $this->request->path() === 'dashboard';
return view('components.navigation', ['active' => $active]);
}
Request, Auth) directly:
public function __construct(
public Request $request,
public UserRepository $users
) {}
with() in Blade to pass data:
@component('navigation', ['color' => 'dark'])
@endcomponent
public function toHtml(): string
{
return view('components.card', [
'title' => 'Slot Example',
]);
}
@component('card')
@slot('content')
{{ $slot }}
@endslot
@endcomponent
__construct:
public function __construct(public Post $post) {}
toHtml():
public function toHtml(): string
{
return Cache::remember("nav_{$this->request->path()}", now()->addHour(), function () {
return view('components.navigation')->render();
});
}
$component = new NavigationComponent(
$mockRequest,
$mockUserRepository
);
$this->assertStringContainsString('Dashboard', $component->toHtml());
$blade = new BladeOne($viewFactory);
$this->assertEquals('<nav>...</nav>', $blade->renderComponent('navigation'));
Archived Package:
Blade Cache Invalidation:
bootstrap/cache after adding new components:
php artisan view:clear
Circular Dependencies:
__construct (use lazy loading).Component Not Found:
config/app.php registration and namespace.@component('name').Missing Dependencies:
Custom Htmlable:
Extend Htmlable for non-string outputs (e.g., JSON):
class ApiComponent implements Htmlable
{
public function toHtml(): string
{
return response()->json(['data' => []])->getContent();
}
}
Dynamic Registration: Register components dynamically in a service provider:
View::addComponent('dynamic', function () {
return new DynamicComponent();
});
Legacy Blade Support:
Use {{ $component->render() }} if toHtml() fails in older Laravel versions.
toHtml:
Offload processing to __construct or services.public function toHtml(): string
{
return Cache::rememberForever('footer_component', function () {
return view('components.footer')->render();
});
}
How can I help you explore Laravel packages today?