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 ViewModel
s (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)