rareloop/view-models
Composer 安装命令:
composer require rareloop/view-models
包简介
A small package to make dealing with views easier
关键字:
README 文档
README
This package is deprecated. View Models are now baked into the Lumberjack core.
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.
For example, for this twig view:
{% 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):
// 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.**
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.
Our address is: 12 Basepoint, Andersons Road, Southampton, Hampshire, SO14 5FE.
Installation
You can install the package via composer:
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.
They should not fetch data from anywhere (e.g. database). This is the job of a ViewModelComposer.
Using an example ViewModel (e.g. in a controller):
$params = new ParameterBag([ 'links' => [ 'https://google.com', 'https://rareloop.com', ], ]); $context['links'] = \App\ViewModels\Links::make($params);
Here is an example ViewModel implementation:
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.
These should be used instead of duplicating logic when creating ViewModels (e.g. in controllers).
Example ViewModelComposer implementation:
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.
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email info@rareloop.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
rareloop/view-models 适用场景与选型建议
rareloop/view-models 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 07 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「view-models」 「rareloop」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rareloop/view-models 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rareloop/view-models 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rareloop/view-models 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 24
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-07