2017 © Pedro Peláez
 

library laravel-view-components

Provide a simple but effective view components system to Laravel.

image

axn/laravel-view-components

Provide a simple but effective view components system to Laravel.

  • Friday, July 20, 2018
  • by axn
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Laravel View Components

Provide a simple but effective view components system to Laravel, similar to view composers but in a different way : while a composer is called when the view is rendered, with view component, the view is rendered by the component itself. So, the logic is more tied to the view., (*1)

Installation

With Composer:, (*2)

composer require axn/laravel-view-components

In Laravel 5.5 the service provider is automatically included. In older versions of the framework, simply add this service provider to the array of providers in config/app.php:, (*3)

// config/app.php

'provider' => [
    //...
    Axn\ViewComponents\ServiceProvider::class,
    //...
];

Usage

Create a class that extends Axn\ViewComponents\ViewComponent and write the logic of your component in the compose() method. The compose() method must return the view contents., (*4)

Example:, (*5)

namespace App\ViewComponents;

use Axn\ViewComponents\ViewComponent;
use App\Models\User;
use LaravelCollective\FormFacade as Form;

class UsersSelect extends ViewComponent
{
    protected function compose()
    {
        $users = User::pluck('username', 'id');

        return Form::select('user_id', $users, null, [
            'class' => 'form-control'
        ]);
    }
}

Then use the @render directive to render the component in a view:, (*6)

<div class="form-group">
    {!! Form::label('user_id', 'Select a user:') !!}
    @render(App\ViewComponents\UsersSelect::class)
</div>

Or the render() helper to render the component elsewhere than in a view:, (*7)

$content = render(App\ViewComponents\UsersSelect::class);

If you need to pass data to the component:, (*8)

<div class="form-group">
    {!! Form::label('user_id', 'Select a user:') !!}
    @render(App\ViewComponents\UsersSelect::class, [
        'selectedUser' => old('user_id', $currentUser->id)
    ])
</div>
$content = render(App\ViewComponents\UsersSelect::class, [
    'selectedUser' => old('user_id', $currentUser->id)
]);

And in the component:, (*9)

//...

class UsersSelect extends ViewComponent
{
    protected function compose()
    {
        $users = User::pluck('username', 'id');

        // $this->selectedUser value comes from the parent view
        return Form::select('user_id', $users, $this->selectedUser, [
            'class' => 'form-control'
        ]);
    }
}

The Versions

20/07 2018

dev-master

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

Provide a simple but effective view components system to Laravel.

  Sources   Download

MIT

The Requires

 

by AXN Informatique

20/07 2018

1.0.0

1.0.0.0 https://github.com/AXN-Informatique/laravel-view-components

Provide a simple but effective view components system to Laravel.

  Sources   Download

MIT

The Requires

 

by AXN Informatique