sfneal / view-models
spatie/view-models 包装器,内置响应缓存。
5.1.2
2024-04-30 19:25 UTC
Requires
- php: ^8.0|^8.1|^8.2|^8.3
- sfneal/caching: ^2.0|^3.0|^4.0
- sfneal/laravel-helpers: ^2.4
- sfneal/queueables: ^2.0
- sfneal/redis-helpers: ^1.2|^2.0|^3.0
- sfneal/string-helpers: ^1.0
- spatie/laravel-view-models: ^1.1
Requires (Dev)
- ext-json: *
- josiasmontag/laravel-redis-mock: >=1.2.6
- orchestra/testbench: ^7.40|^8.0|9.0
- phpunit/phpunit: ^9.6|^10.0|^11.0
- scrutinizer/ocular: ^1.8
README
spatie/view-models 包装器,内置响应缓存
安装
您可以通过 composer 安装此包。
composer require sfneal/view-models
使用方法
视图模型是一个类,您可以在这里放置一些复杂逻辑,以供视图使用。这将使您的控制器更加轻量。您可以通过扩展提供的 Sfneal\ViewModels\AbstractViewModel 来创建视图模型。
use Sfneal\ViewModels\ViewModel; class PostViewModel extends ViewModel { public $indexUrl = null; public function __construct(User $user, Post $post = null) { $this->user = $user; $this->post = $post; $this->indexUrl = action([PostsController::class, 'index']); $this->view = 'your.view'; } public function post(): Post { return $this->post ?? new Post(); } public function categories(): Collection { return Category::canBeUsedBy($this->user)->get(); } }
如下所示在控制器中使用
class PostsController { public function create() { // Uses caching for fast load times after first request return (new PostViewModel(current_user()))->render(); } public function edit(Post $post) { // Doesn't use caching to avoid need for cache invalidation on changes return (new PostViewModel(current_user(), $post))->renderNoCache(); } }
测试
composer test
变更日志
请参阅 CHANGELOG 获取最近更改的更多信息。
贡献
请参阅 CONTRIBUTING 获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 stephen.neal14@gmail.com 联系,而不是使用问题跟踪器。
鸣谢
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 获取更多信息。
PHP 包模板
此包是使用 PHP 包模板 生成的。