2017 © Pedro Peláez
 

library laravel-view-components

A better way to connect data with view rendering in Laravel

image

spatie/laravel-view-components

A better way to connect data with view rendering in Laravel

  • Tuesday, June 5, 2018
  • by Spatie
  • Repository
  • 6 Watchers
  • 136 Stars
  • 1,552 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 8 Versions
  • 2885 % Grown

The README.md

THIS PACKAGE HAS BEEN ABANDONED, (*1)

A better way to connect data with view rendering in Laravel

Latest Version on Packagist GitHub Workflow Status StyleCI Quality Score Total Downloads, (*2)

View components are a way to help organize logic tied to view, similar to view composers., (*3)

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., (*4)

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., (*5)

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

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., (*7)

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., (*8)

Installation

You can install the package via composer:, (*9)

composer require spatie/laravel-view-components

No additional setup necessary. Laravel will automatically discover and register the service provider., (*10)

Optionally you can publish the config file with:, (*11)

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:, (*12)

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)., (*13)

You can choose between referencing the component via a path or a class name., (*14)

@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., (*15)

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., (*16)

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., (*17)

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

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

Additional namespaces

You can register additional namespaces in the namespaces configuration, similar to view paths., (*19)

``` php return [ 'namespaces' => [ 'navigation' => App\Services\Navigation::class, ], ];, (*20)


app/ Services/ Navigation/ Menu.php, (*21)


The above `Menu` component can now be rendered with `@render('navigation::menu')`. ### Testing ``` bash composer test

Changelog

Please see CHANGELOG for more information on what has changed recently., (*22)

Contributing

Please see CONTRIBUTING for details., (*23)

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker., (*24)

Credits

License

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

The Versions

05/06 2018

dev-master

9999999-dev https://github.com/spatie/laravel-view-components

A better way to connect data with view rendering in Laravel

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

spatie laravel-view-components

04/06 2018

1.1.1

1.1.1.0 https://github.com/spatie/laravel-view-components

A better way to connect data with view rendering in Laravel

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

spatie laravel-view-components

04/06 2018

1.1.0

1.1.0.0 https://github.com/spatie/laravel-view-components

A better way to connect data with view rendering in Laravel

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

spatie laravel-view-components

02/06 2018

1.0.1

1.0.1.0 https://github.com/spatie/laravel-view-components

A better way to connect data with view rendering in Laravel

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

spatie laravel-view-components

31/05 2018

1.0.0

1.0.0.0 https://github.com/spatie/laravel-view-components

A better way to connect data with view rendering in Laravel

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

spatie laravel-view-components

31/05 2018

0.2.0

0.2.0.0 https://github.com/spatie/laravel-view-components

A better way to connect data with view rendering in Laravel

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

spatie laravel-view-components

28/05 2018

0.1.0

0.1.0.0 https://github.com/spatie/laravel-view-components

A better way to connect data with view rendering in Laravel

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

spatie laravel-view-components

24/05 2018

0.0.1

0.0.1.0 https://github.com/spatie/laravel-view-components

A better way to connect data with view rendering in Laravel

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

spatie laravel-view-components