什么时候在 Laravel 中使用模型 vs 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35930332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use models vs service in Laravel?
提问by John Roca
I have been using for a while but still I am confuse on what to include in my controllers specifically "When should I use Model and When to Use Service"?
我已经使用了一段时间,但我仍然对控制器中包含的内容感到困惑,特别是“我应该何时使用模型以及何时使用服务”?
I am confused if I am making the right thing.
我很困惑我是否在做正确的事情。
采纳答案by dpak005
I would suggest to never use models directly into the controllers.. Adopt the repository-pattern which enables the use of dependency injection of services into the controller.. Use Service Layer to contain the business logic.. In this way all the related codes would be onto similar layer.
我建议永远不要直接在控制器中使用模型..采用存储库模式,它可以将服务依赖注入到控制器中..使用服务层来包含业务逻辑..这样所有相关的代码都会到相似的层。
Reference for repository pattern:http://heera.it/laravel-repository-pattern#.VuJcVfl97cs
存储库模式参考:http : //heera.it/laravel-repository-pattern#.VuJcVfl97cs
回答by Nonong Re
Please don't use model directly in your controller, unless if you are building a very simple application but for a large application, it would be good to use a repository. Kindly take a look at this tutorial https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/
请不要直接在您的控制器中使用模型,除非您正在构建一个非常简单的应用程序但对于大型应用程序,最好使用存储库。请看一下本教程https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/