Well i'm learning Symfony (3.3) and i'm little confused about Service Container
. In first tutorial the lector show register, login, add post, edit, delete methods in User, Article Controllers. Then in other tutorial, they show same methods but use Service Container (User and Article services) with User and Article interfaces
. So .. what is the best practice for implementation in Services instead of Controllers.

在Symfony中使用服务而不是在控制器中使用服务的最佳做法是什么?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- dqwh26750 2018-08-08 09:19关注
Controller must implement the application logic like check if it's a post request or if a form is submit etc... Never use DQL or any SQL Request directly inside a controller !
EDIT In the example i use a find method inside the controller because it's a Repository method but i parse the result inside
slugify
(my service method)Services contains business logic like formatting phone numbers, parse some data etc... Of course you can inject a repository inside a service and call your methods inside it.
An example:
//This is a fictive example public function indexAction(Request $request) { //Application logic if(!$request->get('id')) { //redirect somewhere by example } $article = $this->getDoctrine() ->getRepository(Article::class) ->find($request->get('id')); //Business Logic $slug = $this->get('my.acme.service')->slugify($article->getTitle()); }
Hope this helps
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报