- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
关闭。这个问题需要更多focused .它目前不接受答案。
想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post .
7年前关闭。
Improve this question
在阅读以下内容后,我一直在为“胖模型,瘦 Controller ”的概念挑选其他开发人员的大脑:
最佳答案
瘦 Controller
您将在 PHP(vanilla 或 Laravel 或 Symfony)中看到的越来越多的是有史以来最瘦的 Controller 。这是您在 Rails 中已经看到的东西,人们也开始将其称为(通过其他一些实践)六边形。你的 Controller 中只需要一行代码,实际上他们说这应该是你所有方法的目标。这是一个例子,是的,比这多一点,但仍然很瘦:
<?php
class PostController extends Controller {
private $repository;
public function __construct(PostRepositoryInterface $repository)
{
$this->repository = $repository;
}
public function store()
{
try
{
$this->repository->create(Input::all());
}
catch (ValidationException $e)
{
return Redirect::back()->withInput()->withErrors($e->all());
}
return Redirect::route('posts');
}
}
<?php
class PostController extends Controller {
private $repository;
public function __construct(PostRepositoryInterface $repository)
{
$this->repository = $repository;
}
public function store()
{
return $this->repository->create(Input::all(), $this);
}
public function createSucceeded()
{
return Redirect::route('posts');
}
public function createFailed()
{
return Redirect::back()->withInput()->withErrors($e->all());
}
}
$this
)来触发
succeeded
和
failed
方法。
class PostRepository implements PostRepositoryInterface {
private $model;
public function __construct(PostInterface $model)
{
$this->model = $model;
}
public function create($input)
{
return $this->model->create($input);
}
public findBySlug($slug)
{
return $this->model->where('slug', $slug)->first();
}
}
Mailer::send($user->id, 'Thanks for registering', 'emails.registered');
<?php namespace ACR\Services\Mailer;
use Illuminate\Mail\Mailer as IlluminateMailer;
use Sentry;
class Service {
public function __construct(IlluminateMailer $mailer)
{
$this->mailer = $mailer;
}
public function send($userId, $subject, $view, $data = [])
{
return $this->mailer->queue($view, $data, function($message) use ($userId, $subject)
{
$user = Sentry::findUserById($userId);
$message->to($user->email, $user->name);
$message->subject($subject);
});
}
}
<?php namespace ACR\Services\Mailer;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
use ACR\Services\Mailer\Service as Mailer;
class ServiceProvider extends IlluminateServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind('acr.mailer', function($app) {
return new Mailer($app->make('mailer'));
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array('acr.mailer');
}
}
<?php namespace ACR\Services\Mailer;
use Illuminate\Support\Facades\Facade as IlluminateFacade;
class Facade extends IlluminateFacade {
protected static function getFacadeAccessor() { return 'acr.mailer'; }
}
interface PostInterface {
public function all();
public function find($id);
}
class DbPost extends Eloquent implements PostInterface {
}
class RedisPost extends Eloquent implements PostInterface {
}
App::bind('PostInterface', 'DbPost');
class PostRepository implements PostRepositoryInterface {
private $model;
public function __construct(PostInterface $model)
{
$this->model = $model;
}
}
App::bind('PostInterface', 'RedisPost');
<?php
class PostController extends Controller {
private $repository;
public function __construct(PostRepositoryInterface $repository)
{
$this->repository = $repository;
}
public function index()
{
return View::make('posts.index')->with('posts', $this->repository->getPaginated());
}
}
@extends('layout')
@section('contents')
<ul>
@foreach($posts as $post)
<li>
{{ $post->title }} - {{ $post->author }} - {{ $post->published_at }}
</li>
@endforeach
</ul>
{{ $users->links() }}
@stop
<?php namespace App\Presenters;
use McCool\LaravelAutoPresenter\BasePresenter;
class Post extends BasePresenter {
public function __construct(UserModel $user)
{
$this->resource = $user;
}
public function author()
{
return $this->resource->author->name;
}
public function published_at()
{
return $this->date($this->resource->created_at);
}
public function dateTime($date)
{
return \Carbon\Carbon::createFromFormat('d-m-Y', $date, 'Sao_Paulo/Brazil')
->toFormattedDateString();
}
}
关于php - 从 "Fat model, skinny controller"的角度使用 Laravel Eloquent ORM 的做法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22566572/
请提及(大)ORM 和微 ORM 之间的区别。与大型 ORM 相比,微型 ORM 有何优势?例如。 Entity Framework ORM 和 dapper micro ORM 之间的区别。 最佳答
我有一个名为 User 的模型。我可以从 User 模型扩展另一个模型 Admin/mod 吗?我找到了续集文档,但我没有找到 最佳答案 是的,查看 Associations documentatio
你对数据访问层有什么建议?使用 Entity Framework 和 Hibernate OR 代码生成器(如 Subsonic、.netTiers、T4 等)等 ORM? 最佳答案 对我来说,这很简
我编写了一个代码生成器,它为给定的 SQL Server/CE 数据库生成 POCO 和存储库。没什么花哨的,只有使用经典 ADO.Net 的简单 CRUD 程序。我的问题是,为什么我应该使用像 L2
我扩展了Android.Application类,以便保持状态(将应用程序用作单例)。 但是,我现在想使用Sugar ORM库简化我的数据库访问,但是Sugar文档(http://satyan.git
我正在阅读 OOP 设计模式和框架设计,发现自己对术语 ORM 和持久性框架之间的区别有点不确定。 ORM 是 PF 的一种吗?您可以期待两者有哪些不同的功能? 最佳答案 我将 ORM 定义为将任何数
在 Jboss EAP 7.0 中加载 orm 文件时,我遇到了以下问题,但它在 Weblogic 中工作。 原因:org.hibernate.boot.MappingException:无法解析显式
我一直在研究 Massive ORM。不要忘记,只是看,我还没有做任何编码。 我对保存交易数据的可能性感兴趣。我已经看到可以在一个事务中在同一个表中保存 20 行。但是当我想在单个交易中保存一个订单并
好的。我刚刚开始在我的 Coldfusion 应用程序中使用 ORM。到现在为止一切都很顺利。我遇到了这个障碍。我有这两张表: 这是我用来将数据加载到页面中的代码。 if 的第二部分是默认加载,第一部
我有可以分配许多类别的帖子。所以它是一个多对多的关系。 我想计算每个类别中有多少帖子(符合特定标准),然后对结果进行排序。 我有: Select ( Select count(post.id)
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 8年前关闭。 Improve t
我正在评估如何使用后端的分布式键/值存储来实现某些东西。我希望在支持对象模型的键/值之上有一个层,该对象模型类似于我从对象关系映射器中获得的模型。 任何人都可以指出其他人这样做的任何例子吗?我主要是在
我正在使用 kohana ORM 以便从数据库中获取一些结果。我的问题是:即使我查阅了文档,我也找不到只选择我感兴趣的列的方法。更明确地说,我有: $sale_stock = Model::facto
在 ORM 之前,如果我想显示规范化表的组合输出,我只需执行快速 CFQUERY,将表连接到我想要的字段并显示输出。我只是无法使用 ORM 来解决它。 例如这两个表: customers (id,
异步 ORM 的真正含义是什么?它与常规 ORM 的行为有何不同? 它可以用在什么地方? 最佳答案 这意味着对它的调用会立即返回(不会阻塞)。您会在稍后的某个时间点获得结果,很可能是由于回调触发。
虽然代码优先非常适合部署和开发,但我看不出如何在投入生产后以代码优先的方式插入对域模型所做的更改。 我如何处理我们在生产过程中积累的数据? 我是否应该手动将数据从 A 版架构迁移到 B 版架构?我是否
像学说(事件记录)和Xyster(数据映射器),有什么区别? 最佳答案 不同之处在于域对象与数据访问层的分离程度。使用 ActiveRecord,它是一个对象,这使它变得非常简单。特别是如果您的类一对
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我在 ColdFusion 中有一个持久实体,我需要更新一个属性 property name="createdDateTime" ormtype="date"; 到 property name="cr
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我是一名优秀的程序员,十分优秀!