1. Go to this page and download the library: Download rareloop/view-models library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rareloop / view-models example snippets
// 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,
];
}
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);
}
}
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);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.