Laravel View Components Package

Published on by

Laravel View Components Package image

We featured View Components in Laravel, written by Jeff Ochoa, as an alternative approach to View Composers.

Inspired by Jeff’s article, Sebastian De Deyne of Spatie created a Laravel package that brings the concept of a View Component to Laravel applications. The package headline is: “A better way to connect data with view rendering in Laravel.”

One of the nice things about this approach over using view composers, is that view components help organize logic and data tied to a view in one place:

The benefit over view composers is that data and rendering logic are explicitly tied together in components instead of being connected afterwards. They also allow you to seamlessly combine properties and dependency injection.

Overview

The overall concept of a view component, is using the @render directive in a template. For example:

@render(App\Http\ViewComponents\NavigationComponent::class, ['backgroundColor' => 'black'])

The first argument in the @render directive ties back to a class that implements the HtmlAble contract. For example, the following component is from the package’s readme:

namespace App\Http\ViewComponents;
 
use Illuminate\Http\Request;
use Illuminate\Contracts\Support\Htmlable;
 
class NavigationComponent implements Htmlable
{
/** \Illuminate\Http\Request */
private $request;
 
/** @var string */
private $backgroundColor;
 
public function __construct(Request $request, string $backgroundColor)
{
$this->request = $request;
$this->backgroundColor = $backgroundColor;
}
 
public function toHtml(): string
{
return view('components.navigation', [
'activeUrl' => $this->request->url(),
'backgroundColor' => $this->backgroundColor,
]);
}
}

Rendering

You can render view components by referencing the component’s fully qualified name (as seen above), or a string class name:

@render('navigationComponent', ['backgroundColor' => 'black'])

Since the component can be anything that implements the HtmlAble contract, you are not required to only using blade views to render components. Check the package’s readme for an example of wrapping an HTML package in a component.

Learn More

Check out the GitHub project to learn more about using this package. I’d also encourage you to read the original post on view components in Laravel that inspired this package.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Forge
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
Bacancy - Staff Augmentation logo

Bacancy - Staff Augmentation

Leave your web app development hustles to the leading IT Staff Augmentation Service Providers. Choose from an extensive pool of 1050+ developers and give yourself the sigh of success you deserve with Bacancy. Get In Touch Today!

Bacancy - Staff Augmentation
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Add Kanban Boards to Your Laravel App in Seconds image

Add Kanban Boards to Your Laravel App in Seconds

Read article
October CMS v3.6 Ships Today, Full of New Features image

October CMS v3.6 Ships Today, Full of New Features

Read article
Laracon EU Videos are now out image

Laracon EU Videos are now out

Read article
Use Google's Gemini AI in Laravel image

Use Google's Gemini AI in Laravel

Read article
PhpStorm is getting a brand new terminal image

PhpStorm is getting a brand new terminal

Read article
Six Essential Plugins for Visual Studio Code image

Six Essential Plugins for Visual Studio Code

Read article