菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
86
0

Taylor Otwell 主张胖模型 / 瘦控制器

原创
05/13 14:22
阅读数 1165

Laravel

I was listening to recent podcast by Taylor Otwell, Laravel Snippet episode 11, where he touched on the debate in Laravel community on where you should put your code logic – in Controllers, Models, or Services. Here’s what he said.

First, I’ll just quote Taylor, and then provide some comments and additional links below. So, “skinny” or “fat” models? What approach does creator of Laravel prefer?

*I tend to prefer a “fat” Model approach. My Controllers are actually pretty skinny, I find.

I think DHH (David Heinemeier Hansson), who created Ruby on Rails, had a blog post years ago about how he likes to use only the “resource” verbs on Controllers and that he doesn’t like any other actions. So, when you have a Controller, you limit yourself to only index/show/store/update/delete, and if you need any other actions – usually that’s an indicator that you can extract the whole other Controller that follows those resource verbs conventions.

So I follow that pretty strictly, which, I find, makes my Controllers pretty thin, and then I would have pretty beefy Models that have pretty rich logic on them.

I think one misconception of this approach is that just because your Model has quite a bit of logic exposed in its public API, that doesn’t mean that all the code to perform those tasks has to be within the Model.

So, take Forge for example, just assume a hypothetical Site Model, that has a deploy() method. Of course, sticking the entirety of the deployment logic within the Site Model is probably not even possible, for starters, and probably wouldn’t be a good idea, cause it would be quite a bit of logic. But, what you could do, is still have that public method on the Site Model, but have that call another method, or another Service, or fire off some Command or Queue Job, to actually perform the task.

So, the Model is not necessarily “fat” in terms of lines of code, but it just has a rich API of public methods that give you a fluent nice interface, that is very readable. It’s very readable to have Site deploy(), even if behind the scenes it’s firing off other things. It keeps the public API really nice.*

What I do like, though, from Taylor’s opinion, is to have custom methods in Models. For a long time, there was a debate in community, that Eloquent Models need to have only Eloquent stuff – settings like $table or $fillables, methods for RelationshipsAccessors/Mutators, and nothing more.
But if the creator of the framework himself says that you can put whatever you want there, as long as it doesn’t include all the logic, I agree it’s more readable than calling Service/Job directly from Controller.

What do you think? Agree with Taylor? How “thin” are your Controllers and Models?

You can listen to the original Laravel Snippet #11 podcast here, this topic starts at around 6:20 minute.

本文章首发在 猿圈.com 网站上。

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。


Laravel

I was listening to recent podcast by Taylor Otwell, Laravel Snippet episode 11, where he touched on the debate in Laravel community on where you should put your code logic – in Controllers, Models, or Services. Here’s what he said.

First, I’ll just quote Taylor, and then provide some comments and additional links below. So, “skinny” or “fat” models? What approach does creator of Laravel prefer?

*I tend to prefer a “fat” Model approach. My Controllers are actually pretty skinny, I find.

I think DHH (David Heinemeier Hansson), who created Ruby on Rails, had a blog post years ago about how he likes to use only the “resource” verbs on Controllers and that he doesn’t like any other actions. So, when you have a Controller, you limit yourself to only index/show/store/update/delete, and if you need any other actions – usually that’s an indicator that you can extract the whole other Controller that follows those resource verbs conventions.

So I follow that pretty strictly, which, I find, makes my Controllers pretty thin, and then I would have pretty beefy Models that have pretty rich logic on them.

I think one misconception of this approach is that just because your Model has quite a bit of logic exposed in its public API, that doesn’t mean that all the code to perform those tasks has to be within the Model.

So, take Forge for example, just assume a hypothetical Site Model, that has a deploy() method. Of course, sticking the entirety of the deployment logic within the Site Model is probably not even possible, for starters, and probably wouldn’t be a good idea, cause it would be quite a bit of logic. But, what you could do, is still have that public method on the Site Model, but have that call another method, or another Service, or fire off some Command or Queue Job, to actually perform the task.

So, the Model is not necessarily “fat” in terms of lines of code, but it just has a rich API of public methods that give you a fluent nice interface, that is very readable. It’s very readable to have Site deploy(), even if behind the scenes it’s firing off other things. It keeps the public API really nice.*

What I do like, though, from Taylor’s opinion, is to have custom methods in Models. For a long time, there was a debate in community, that Eloquent Models need to have only Eloquent stuff – settings like $table or $fillables, methods for RelationshipsAccessors/Mutators, and nothing more.
But if the creator of the framework himself says that you can put whatever you want there, as long as it doesn’t include all the logic, I agree it’s more readable than calling Service/Job directly from Controller.

What do you think? Agree with Taylor? How “thin” are your Controllers and Models?

You can listen to the original Laravel Snippet #11 podcast here, this topic starts at around 6:20 minute.

本文章首发在 猿圈.com 网站上。

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

发表评论

0/200
86 点赞
0 评论
收藏
为你推荐 换一批