engageinteractive/laravel-view-models
Composer 安装命令:
composer require engageinteractive/laravel-view-models
包简介
Use view models instead of database model in your blade files and JSON responses.
README 文档
README
A straight forward pattern for using mappers/view models instead of database model in your blade files and JSON responses.
Installation
composer require engageinteractive/laravel-view-models
Mappers
Create a mapper that can build view models for your Eloquent model:
namespace App\Domain\Posts; use EngageInteractive\LaravelViewModels\Mapper; use App\Domain\Posts\Post; class PostShowMapper extends Mapper { /** * Map a Post to a basic PHP array. * * @param \App\Domain\Posts\Post * @return array */ public function map(Post $post) { return $post->only('title', 'author_name'); } }
Ask for an instance of the Mapper in your controller via the container:
namespace App\Domain\Posts; use App\Domain\Posts\Post; use App\Domain\Posts\PostMapper; use App\Http\Controllers\Controller; class PostController extends Controller { /** * Show a Post. * * @param \App\Domain\Posts\Post * @param \App\Domain\Posts\PostMapper * @return \Illuminate\Views\View */ public function show(Post $post, PostShowMapper $model) { return view('post.show', [ 'model' => $mapper->one($post), ]), } }
View models
Create a view model, to build data to pass into your view:
namespace App\Domain\Posts; use EngageInteractive\LaravelViewModels\ViewModel; use Illuminate\Support\Str; class PostViewModel extends ViewModel { protected $post; /** * Intialise the View ViewModel. * * @param \App\Domain\Posts\Post * @return void */ public function __construct(Post $post): void { $this->post = $post; } /** * Returns the post title in title-case. * * @return string */ public function postTitle(): string { if (!isset($this->post->title)) { return 'Untitled'; } return Str::title($this->post->title); } }
Pass the ViewModel array into the view:
namespace App\Domain\Posts; use App\Domain\Posts\Post; use App\Domain\Posts\PostViewModel; use App\Http\Controllers\Controller; class PostController extends Controller { /** * Show a Post. * * @param \App\Domain\Posts\Post * @return \Illuminate\Views\View */ public function show(Post $post) { $model = new PostViewModel($post); return view('post.show', $model->array()), } }
Below is an example of the data passed into the view:
[
'model' => [
'post_title' => 'This Is The Title',
],
]
Combining View models and Mappers
First, create a mapper for posts.
namespace App\Domain\Posts; use EngageInteractive\LaravelViewModels\Mapper; use App\Domain\Posts\Post; class PostsMapper extends Mapper { /** * Map a Post to a basic PHP array. * * @param \App\Domain\Posts\Post * @return array */ public function map(Post $post) { return $post->only('title', 'author_name'); } }
Create a ViewModel, and call the mapper.
namespace App\Domain\Posts; use EngageInteractive\LaravelViewModels\ViewModel; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Str; class PostArchiveViewModel extends ViewModel { protected $posts; /** * Initialise the View model. * * @param \Illuminate\Database\Eloquent\Collection * @return void */ public function __construct(Collection $posts): void { $this->posts = $posts; } /** * Returns an array of posts. * * @return string */ public function posts(): array { return (new PostsMapper)->all($this->posts); } /** * Returns the application home URI. * * @return string */ public function HomeUri(): string { return route('home'); } }
Pass the view model into the view, with the posts mapped into the required format.
namespace App\Domain\Posts; use App\Domain\Posts\Post; use App\Domain\Posts\PostArchiveViewModel; use App\Http\Controllers\Controller; class PostArchiveController extends Controller { /** * Show a Post. * * @return \Illuminate\Views\View */ public function show() { $posts = Post::all(); $model = new PostArchiveViewModel($posts); return view('post-archive.show', $model->array()), } }
Laravel Compatibility
Works on Laravel 5.5+.
License
Laravel View Models is open-sourced software licensed under the MIT license.
engageinteractive/laravel-view-models 适用场景与选型建议
engageinteractive/laravel-view-models 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.87k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 03 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 engageinteractive/laravel-view-models 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 engageinteractive/laravel-view-models 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 12.87k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-29