laravel-commode/viewmodel
Composer 安装命令:
composer require laravel-commode/viewmodel
包简介
ViewModel approach for laravel 5.(=>1)
README 文档
README
#Commode: ViewModel
laravel-commode/viewmodel is an implementation of ViewModel approach for laravel framework.
####Contents
You can install laravel-commode/viewmodel using composer:
"require": {
"laravel-commode/viewmodel": "dev-master"
}
To enable package you need to register LaravelCommode\ViewModel\ViewModelServiceProvider
service provider in your application config.
<?php
// ./yourLaravelApplication/app/config/app.php
return [
// ... config code
'providers' => [
// ... providers
'LaravelCommode\ViewModel\ViewModelServiceProvider'
]
];
##Why should I bother?
I could bet that there many laravel users, that would ask this question:
"Why should I bother about viewmodel approach, since I have
Eloquentmodels andInputfacade?".
As well, as I bet that this category of users have never used repository/service/strategy pattern approaches.
First of all I'd like to say that Eloquent is not a model. It's an awesome way to communicate with database as
an ActiveRecord pattern implementation. But still there are issues that every eloquent user has to face, like
properties encapsulated in $attributes array.
The other thing is that it's really bad aggregating your application input data in your ActiveRecord model,
since data aggregation inside ActiveRecord pattern can bring a lot of pain when your application gets bigger
and bigger especially if was is not abstracted - it becomes hardly readable, testable and not context reliable.
Imagine if once you decided to move to Doctrine usage what headache would it be to refactor all data
aggregating/pulling/storing.
So, as a conclusion, I could say that ViewModel brings another abstraction layer between user and database interaction.
This package provides two basic ViewModel types: FileViewModel for using it only with files, and
ViewModel for using as with files, as with common values.
So let's say you would have to implement context oriented model for Profile model, because it's the most
common example for context oriented models: it's used in you acl, it's used in your admin panels, it's used by
your users, e.t.c. ... And almost each time it required different validators, different data aggregation
processes before it's being stored or rejected to be stored in your database and so on.
To create your ViewModel you need to extend LaravelCommode\ViewModel\ViewModels\ViewModel and implement
two protected methods:
getBaseModel($attributes = array())- will receive an associative array of attributes - current values of ViewModel's public properties. Method must return model which your ViewModel can be converted to. That might be very useful for casting to other object.
getValidationObject($data = [], $isNew = true)- will receive an associative array of attributes - current values of ViewModel's public properties and a boolean flag that indicates if model is being created or updated. Method must return laravel validator instance.
For example:
<?php namespace MyApp\Domain\Admin\ViewModels;
use LaravelCommode\ViewModel\ViewModels\ViewModel;
class Profile extends ViewModel
{
public $login;
public $password;
public $password_confirmation;
public $email;
public $firstname;
public $lastname;
protected function getBaseModel($attributes = array())
{
$model = new \MyApp\DAL\Eloquent\Models\Profile();
$model->fill($attributes);
return $model;
}
protected function getValidationObject($data = [], $isNew = true)
{
$rules = [
'login' => 'required',
'email' => 'required',
'firstname' => 'required',
'lastname' => 'required'
];
if ($isNew)
{
$rules['password'] = 'required|confirmed';
}
return \Validator::make($data, $rules);
}
}
laravel-commode/viewmodel 适用场景与选型建议
laravel-commode/viewmodel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27 次下载、GitHub Stars 达 4, 最近一次更新时间为 2014 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 laravel-commode/viewmodel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laravel-commode/viewmodel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 27
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-11-26