takemo101/laravel-simple-vm
Composer 安装命令:
composer require takemo101/laravel-simple-vm
包简介
Laravel Simple VM
关键字:
README 文档
README
This package is a wrap of Simple VM for Laravel.
You can also create a ViewModel class with a command.
Enjoy!
Installation
Execute the following composer command.
composer require takemo101/laravel-simple-vm
How to use
Please use as follows
Create a ViewModel class
Execute the artisan command as below.
# php artisan make:svm TestViewModel
Basic
1. Create a ViewModel class
First, create a ViewModel class to output the data to View.
<?php namespace App\Http\ViewModel; use Takemo101\LaravelSimpleVM\ViewModel; use Takemo101\SimpleVM\Attribute\{ Ignore, ChangeName, }; use App\Models\User; use Illuminate\Support\Collection; /** * You need to extend and create the ViewModel class. * Only the public properties and methods defined in the ViewModel class will be the output target values. */ class TestViewModel extends ViewModel { /** * ignores property or method names * * @var string[] */ protected array $__ignores = [ // ]; /** * The data to be output to View is passed to the ViewModel class in the constructor. */ public function __construct( public string $description, private User $user, ) { // } /** * Method injection is available for each published method */ public function users(User $user): Collection { return $user->all(); } /** * You can also process the model object and output it like the JsonResponse class. */ public function user(): array { return [ 'name' => $this->user->name, 'email' => $this->user->email, ]; } /** * You can ignore the output to the View by setting the Ignore Attribute class */ #[Ignore] public function other(): string { return 'other'; } /** * You can change the output name to the View by setting the ChangeName Attribute class. */ #[ChangeName('modification')] public function change(): string { return 'change'; } }
2. Use ViewModel class in Controller class
Next, use the created ViewModel class object as a response in the controller.
<?php namespace App\Http\Controllers; use App\Http\ViewModel\TestViewModel; use App\Models\User; class HomeController { /** * Output Json by making the return value of the controller method an object of ViewModel */ public function json() { $user = new User(); $user->name = 'name'; $user->email = 'xxx@xxx.com'; return new TestViewModel( 'description', $user, ); } /** * You can use the output data on the template by passing a ViewModel object as the template data */ public function view() { $user = new User(); $user->name = 'name'; $user->email = 'xxx@xxx.com'; // By using the toAccessArray method, you can treat the output data like an object on the template. return view('home.view', (new TestViewModel( 'description', $user, ))->toAccessArray()); // You can create an object from the of method of the ViewModel class } }
Below is the output result in Json.
{
"description": "description",
"users": [],
"user": {
"name": "name",
"email": "xxx@xxx.com"
},
"modification": "change"
}
takemo101/laravel-simple-vm 适用场景与选型建议
takemo101/laravel-simple-vm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 577 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 02 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「php」 「laravel」 「View Model」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 takemo101/laravel-simple-vm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 takemo101/laravel-simple-vm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 takemo101/laravel-simple-vm 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple ASCII output of array data
Alfabank REST API integration
Package for view storage in laravel
User Approval Laravel Package
PHPUnit Pretty Result Printer
统计信息
- 总下载量: 577
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-22