2017 © Pedro Peláez
 

library view-models

A small package to make dealing with views easier

image

rareloop/view-models

A small package to make dealing with views easier

  • Tuesday, June 5, 2018
  • by rareloop
  • Repository
  • 1 Watchers
  • 0 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

This package is deprecated. View Models are now baked into the Lumberjack core., (*1)

View Models

Here, a ViewModel refers to something that takes data and transforms it into the correct format for a specific view. They are input-output machines., (*2)

For example, for this twig view:, (*3)

{% for link in links %}
    <a href="{{ link.url }}">{{ link.title }}</a>
{% endfor %}

You will need to construct an array that looks like this (e.g. in your controller):, (*4)

// Get pages from the database somehow
$pages = Page::all();

$data = ['links' => []];

foreach ($pages as $page) {
    // Map the page to the correct structure for the view
    $data['links'][] = [
        'url' => $page->permalink,
        'title' => $page->post_title,
    ];
}

** ViewModels abstract away that transformation. This means you don't have to duplicate that transformation logic across multiple controllers, making your code eaiser to change.**, (*5)

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using., (*6)

Our address is: 12 Basepoint, Andersons Road, Southampton, Hampshire, SO14 5FE., (*7)

Installation

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

composer require rareloop/view-models

View Model Usage

They should always return an array. The easiest way to achieve this is to run your data through the compose method on the View Model., (*9)

They should not fetch data from anywhere (e.g. database). This is the job of a ViewModelComposer., (*10)

Using an example ViewModel (e.g. in a controller):, (*11)

``` php $params = new ParameterBag([ 'links' => [ 'https://google.com', 'https://rareloop.com', ], ]);, (*12)

$context['links'] = \App\ViewModels\Links::make($params);, (*13)


Here is an example `ViewModel` implementation: ```php namespace App\ViewModels\Links; use Rareloop\ViewModels\ParameterBag; use Rareloop\ViewModels\ViewModel; class Links extends ViewModel { public static function make(ParameterBag $params): array { // Transform the data into the correct structure $data = array_map(function ($item) { return [ 'url' => $item['ID'], ]; }, $params->links); // Make sure the data is an array return static::compose($data); } }

Introducing View Model Composers

A ViewModelComposer is simply a wrapper around a ViewModel, but is only concerned how to get data ready for a ViewModel., (*14)

These should be used instead of duplicating logic when creating ViewModels (e.g. in controllers)., (*15)

Example ViewModelComposer implementation:, (*16)

class RelatedLinks
{
    public static function make(): array
    {
        // e.g. you could get the data out of the database for the related links for this page
        $args = new ParameterBag([
            'links' => [
                'http://google.com',
                'https://rareloop.com',
            ],
        ]);

        return Links::make($args);
    }
}

Changelog

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

Testing

bash composer test, (*18)

Contributing

Please see CONTRIBUTING for details., (*19)

Security

If you discover any security related issues, please email info@rareloop.com instead of using the issue tracker., (*20)

License

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

The Versions

05/06 2018

dev-master

9999999-dev https://github.com/Rareloop/view-models

A small package to make dealing with views easier

  Sources   Download

MIT

The Requires

 

The Development Requires

view-models rareloop

07/07 2017

v1.0.1

1.0.1.0 https://github.com/Rareloop/view-models

A small package to make dealing with views easier

  Sources   Download

MIT

The Requires

 

The Development Requires

view-models rareloop

07/07 2017

v1.0.0

1.0.0.0 https://github.com/rareloop/view-models

:package_description

  Sources   Download

MIT

The Requires

 

The Development Requires

view-models rareloop