spatie/laravel-view-models
Composer 安装命令:
composer require spatie/laravel-view-models
包简介
View models in Laravel
README 文档
README
Have you ever made a controller where you had to do a lot of work to prepare variables to be passed to a view? You can move that kind of work to a so called view model. In essence, view models are simple classes that take some data, and transform it into something usable for the view.
You'll find a more detailed explanation and some good examples in this blogpost on Stitcher.io.
Laravel's native view composers are not the same as the view models provided by this package. To learn more about the differences head over to this blogpost on Stitcher.io.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/laravel-view-models
Usage
A view model is a class where you can put some complex logic for your views. This will make your controllers a bit lighter. You can create a view model by extending the provided Spatie\ViewModels\ViewModel.
class PostViewModel extends ViewModel { public $user; public $post; public $indexUrl = null; public function __construct(User $user, Post $post = null) { $this->user = $user; $this->post = $post; $this->indexUrl = action([PostsController::class, 'index']); } public function post(): Post { return $this->post ?? new Post(); } public function categories(): Collection { return Category::canBeUsedBy($this->user)->get(); } }
Then you can use the view model class in your controller like this:
class PostsController { public function create() { $viewModel = new PostViewModel( current_user() ); return view('blog.form', $viewModel); } public function edit(Post $post) { $viewModel = new PostViewModel( current_user(), $post ); return view('blog.form', $viewModel); } }
In a view you can do this:
<input type="text" value="{{ $post->title }}" /> <input type="text" value="{{ $post->body }}" /> <select> @foreach ($categories as $category) <option value="{{ $category->id }}">{{ $category->name }}</option> @endforeach </select> <a href="{{ $indexUrl }}">Back</a>
All public methods and properties in a view model are automatically exposed to the view. If you don't want a specific method to be available in your view, you can ignore it.
class PostViewModel extends ViewModel { protected $ignore = ['ignoredMethod']; // … public function ignoredMethod() { /* … */ } }
All PHP's built in magic methods are ignored automatically.
View models as responses
It's possible to directly return a view model from a controller. By default, a JSON response with the data is returned.
class PostsController { public function update(Request $request, Post $post) { // … return new PostViewModel($post); } }
This approach can be useful when working with AJAX submitted forms.
It's also possible to return a view directly:
class PostsController { public function update(Request $request, Post $post) { // … return (new PostViewModel($post))->view('post.form'); } }
Note that when the Content-Type header of the request is set to JSON,
this approach will also return JSON data instead of a rendered view.
Exposing view functions
View models can expose functions which require extra parameters.
class PostViewModel extends ViewModel { public function formatDate(Carbon $date): string { return $date->format('Y-m-d'); } }
You can use these functions in the view like so:
{{ $formatDate($post->created_at) }}
Making a new view model
The package included an artisan command to create a new view model.
php artisan make:view-model HomepageViewModel
This view model will have the App\ViewModels namespace and will be saved in app/ViewModels.
or into a custom namespace, say, App\Blog
php artisan make:view-model "Blog/PostsViewModel"
This view model will have the App\Blog\ViewModels namespace and will be saved in app/Blog/ViewModels.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
spatie/laravel-view-models 适用场景与选型建议
spatie/laravel-view-models 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.71M 次下载、GitHub Stars 达 1.09k, 最近一次更新时间为 2018 年 09 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「spatie」 「laravel-view-models」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/laravel-view-models 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/laravel-view-models 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/laravel-view-models 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
Easily optimize images using PHP
Store your application settings
Add tags and taggable behaviour to your Laravel app
Speed up a Laravel application by caching the entire response additions
Manage events on a Google Calendar
统计信息
- 总下载量: 3.71M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1096
- 点击次数: 10
- 依赖项目数: 13
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-12