All Projects → spatie → Laravel View Components

spatie / Laravel View Components

Licence: mit
A better way to connect data with view rendering in Laravel

Projects that are alternatives of or similar to Laravel View Components

Laravel Blade X
Use custom HTML components in your Blade views
Stars: ✭ 538 (+126.05%)
Mutual labels:  laravel, rendering, components
Laravel Adminlte
Easy AdminLTE integration with Laravel
Stars: ✭ 2,990 (+1156.3%)
Mutual labels:  laravel
Tracker
Laravel Stats Tracker
Stars: ✭ 2,638 (+1008.4%)
Mutual labels:  laravel
Laravelcs
Laravel PHP_CodeSniffer
Stars: ✭ 232 (-2.52%)
Mutual labels:  laravel
Laravel Query Monitor
Simple artisan command to monitoring triggered queries
Stars: ✭ 230 (-3.36%)
Mutual labels:  laravel
One Loader
Single-file components for React
Stars: ✭ 233 (-2.1%)
Mutual labels:  components
Laravelmetatags
The most powerful and extendable tools for managing SEO Meta Tags in your Laravel project
Stars: ✭ 226 (-5.04%)
Mutual labels:  laravel
Laravel Twbs4
Laravel 5 frontend preset for Twitter Bootstrap 4
Stars: ✭ 237 (-0.42%)
Mutual labels:  laravel
Laravel Comments
Add comments to your Laravel application
Stars: ✭ 234 (-1.68%)
Mutual labels:  laravel
Lang.js
🎭 Laravel Translator class in JavaScript!
Stars: ✭ 232 (-2.52%)
Mutual labels:  laravel
Laravel Gentelella
A Laravel 5.4 application with Gentelella bootstrap admin tempalte
Stars: ✭ 232 (-2.52%)
Mutual labels:  laravel
Auth Tests
Always-current tests for Laravel's authentication system. Curated by the community.
Stars: ✭ 230 (-3.36%)
Mutual labels:  laravel
Times Components
A collection of reusable components used by The Times
Stars: ✭ 232 (-2.52%)
Mutual labels:  components
Laravel Auth
Laravel 8 with user authentication, registration with email confirmation, social media authentication, password recovery, and captcha protection. Uses offical [Bootstrap 4](http://getbootstrap.com). This also makes full use of Controllers for the routes, templates for the views, and makes use of middleware for routing. The project can be stood u…
Stars: ✭ 2,692 (+1031.09%)
Mutual labels:  laravel
Smart Hierarchy
Better hierarchy for Unity.
Stars: ✭ 234 (-1.68%)
Mutual labels:  components
Tomatoidc
TomatoIDC是一款以MIT协议开源主机销售系统,具备易于扩展的插件系统,模版系统,使用强大的Laravel框架进行驱动,能帮助你轻松的扩展主机销售业务。
Stars: ✭ 230 (-3.36%)
Mutual labels:  laravel
Laravel Cronless Schedule
Run the Laravel scheduler without relying on cron
Stars: ✭ 231 (-2.94%)
Mutual labels:  laravel
Vue 2 3
↔️ Interop Vue 2 components in Vue 3 apps and vice versa
Stars: ✭ 231 (-2.94%)
Mutual labels:  components
Gistlog
GistLog - simple, easy blogging based on GitHub gists
Stars: ✭ 237 (-0.42%)
Mutual labels:  laravel
A17t
An atomic design toolkit for pragmatists
Stars: ✭ 236 (-0.84%)
Mutual labels:  components

THIS PACKAGE HAS BEEN ABANDONED

A better way to connect data with view rendering in Laravel

Latest Version on Packagist GitHub Workflow Status StyleCI Quality Score Total Downloads

View components are a way to help organize logic tied to view, similar to view composers.

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,
        ]);
    }
}
@render('navigationComponent', ['backgroundColor' => 'black'])

A view component can be anything that implements Laravel's Htmlable contract, so you don't necessarily need to use Blade views to render the component. This is useful for wrapping third party HTML packages, like spatie/laravel-menu.

namespace App\Http\ViewComponents;

use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\Auth\Guard;
use Spatie\Menu\Laravel\Menu;

class MainMenuComponent implements Htmlable
{
    /** @var \Illuminate\Contracts\Auth\Guard */
    private $guard;

    /** @var string */
    private $class;

    public function __construct(Guard $guard, string $class = null)
    {
        $this->guard = $guard;
        $this->class = $class;
    }

    public function toHtml(): string
    {
        $menu = Menu::new()
            ->addClass($this->class)
            ->url('/', 'Home')
            ->url('/projects', 'Projects');

        if ($this->guard->check()) {
            $menu->url('/admin', 'Adminland');
        }

        return $menu;
    }
}
@render('mainMenuComponent', ['class' => 'background-green'])

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.

This package is based on Introducing View Components in Laravel, an alternative to View Composers by Jeff Ochoa.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-view-components

No additional setup necessary. Laravel will automatically discover and register the service provider.

Optionally you can publish the config file with:

php artisan vendor:publish --provider="Spatie\ViewComponents\ViewComponentsServiceProvider" --tag="config"

This is the default content of the file that will be published at config/view-components:

return [
    /*
     * The root namespace where components reside. Components can be referenced
     * with camelCase & dot notation.
     *
     * Example: 'root_namespace' => App\Http\ViewComponents::class
     *
     * `@render('myComponent')
     *     => `App\Http\ViewComponents\MyComponent`
     */
    'root_namespace' => 'App\Http\ViewComponents',

    /*
     * Register alternative namespaces here, similar to custom view paths.
     *
     * Example: 'navigation' => App\Services\Navigation::class,
     *
     * `@render('navigation::mainMenu')`
     *     => `App\Services\Navigation\MainMenu`
     */
    'namespaces' => [
        // 'navigation' => App\Services\Navigation::class,
    ],
];

Usage

The @render directive

The @render Blade directive accepts two arguments: the first is the view component's path or class name, the second is an extra set of properties (optional).

You can choose between referencing the component via a path or a class name.

@render('myComponent')
@render(App\Http\ViewComponents\MyComponent::class)

Parameters will be injected in the view components __construct method. The component is instantiated with Laravel's container, so parameters that aren't provided by render will be automatically injected.

use Illuminate\Http\Request;

class MyComponent implements Htmlable
{
    public function __construct(Request $request, string $color)
    {
        $this->request = $request;
        $this->color = $color;
    }

    // ...
}
@render('myComponent', ['color' => 'red'])

In the above example, $color is explicitly set, and a $request object will be injected by Laravel.

Configuration

The root namespace

By configuring root_namespace, you can define where the bulk of your view components are located. By default, this is in App\Http\ViewComponents.

app/
  Http/
    ViewComponents/
      MyComponent.php
      Nested/
        NestedComponent.php

The above components can be rendered with @render('myComponent') and @render('nested.nestedComponent').

Additional namespaces

You can register additional namespaces in the namespaces configuration, similar to view paths.

return [
    'namespaces' => [
        'navigation' => App\Services\Navigation::class,
    ],
];
app/
  Services/
    Navigation/
      Menu.php

The above Menu component can now be rendered with @render('navigation::menu').

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].