{"id":18771036,"url":"https://github.com/rareloop/view-models","last_synced_at":"2025-12-12T03:30:14.125Z","repository":{"id":62533340,"uuid":"96549814","full_name":"Rareloop/view-models","owner":"Rareloop","description":null,"archived":false,"fork":false,"pushed_at":"2018-06-05T08:33:28.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-29T07:43:14.225Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Rareloop.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-07T15:05:17.000Z","updated_at":"2018-06-05T08:33:29.000Z","dependencies_parsed_at":"2022-11-02T15:00:21.517Z","dependency_job_id":null,"html_url":"https://github.com/Rareloop/view-models","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rareloop%2Fview-models","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rareloop%2Fview-models/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rareloop%2Fview-models/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rareloop%2Fview-models/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Rareloop","download_url":"https://codeload.github.com/Rareloop/view-models/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239678103,"owners_count":19679202,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T19:22:56.704Z","updated_at":"2025-12-12T03:30:14.070Z","avatar_url":"https://github.com/Rareloop.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"**This package is deprecated. View Models are now baked into the Lumberjack core.**\n\n# View Models\n\nHere, a `ViewModel` refers to something that takes data and transforms it into the correct format for a specific view. They are **input-output machines**.\n\nFor example, for this `twig` view:\n\n```twig\n{% for link in links %}\n    \u003ca href=\"{{ link.url }}\"\u003e{{ link.title }}\u003c/a\u003e\n{% endfor %}\n```\n\nYou will need to construct an array that looks like this (e.g. in your controller):\n\n```php\n// Get pages from the database somehow\n$pages = Page::all();\n\n$data = ['links' =\u003e []];\n\nforeach ($pages as $page) {\n    // Map the page to the correct structure for the view\n    $data['links'][] = [\n        'url' =\u003e $page-\u003epermalink,\n        'title' =\u003e $page-\u003epost_title,\n    ];\n}\n```\n\n**  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.**\n\n## Postcardware\n\nYou're free to use this package (it's [MIT-licensed](LICENSE.md)), 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.\n\nOur address is: 12 Basepoint, Andersons Road, Southampton, Hampshire, SO14 5FE.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require rareloop/view-models\n```\n\n### View Model Usage\n\nThey should always return an array. The easiest way to achieve this is to run your data through the `compose` method on the View Model.\n\nThey should **not** fetch data from anywhere (e.g. database). This is the job of a `ViewModelComposer`.\n\nUsing an example `ViewModel` (e.g. in a controller):\n\n``` php\n$params = new ParameterBag([\n    'links' =\u003e [\n        'https://google.com',\n        'https://rareloop.com',\n    ],\n]);\n\n$context['links'] = \\App\\ViewModels\\Links::make($params);\n```\n\nHere is an example `ViewModel` implementation:\n\n```php\nnamespace App\\ViewModels\\Links;\n\nuse Rareloop\\ViewModels\\ParameterBag;\nuse Rareloop\\ViewModels\\ViewModel;\n\nclass Links extends ViewModel\n{\n    public static function make(ParameterBag $params): array\n    {\n        // Transform the data into the correct structure\n        $data = array_map(function ($item) {\n            return [\n                'url' =\u003e $item['ID'],\n            ];\n        }, $params-\u003elinks);\n\n        // Make sure the data is an array\n        return static::compose($data);\n    }\n}\n```\n\n### Introducing View Model Composers\n\nA `ViewModelComposer` is simply a wrapper around a `ViewModel`, but is only concerned how to get data ready for a `ViewModel`.\n\nThese should be used instead of duplicating logic when creating `ViewModel`s (e.g. in controllers).\n\nExample `ViewModelComposer` implementation:\n\n```php\nclass RelatedLinks\n{\n    public static function make(): array\n    {\n        // e.g. you could get the data out of the database for the related links for this page\n        $args = new ParameterBag([\n            'links' =\u003e [\n                'http://google.com',\n                'https://rareloop.com',\n            ],\n        ]);\n\n        return Links::make($args);\n    }\n}\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n``` bash\ncomposer test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Security\n\nIf you discover any security related issues, please email info@rareloop.com instead of using the issue tracker.\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frareloop%2Fview-models","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frareloop%2Fview-models","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frareloop%2Fview-models/lists"}