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
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 metadata提取的PDF元数据,如何转换为一个Excel
- ¥15 关于arduino编程toCharArray()函数的使用
- ¥100 vc++混合CEF采用CLR方式编译报错
- ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
- ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
- ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
- ¥50 如何openEuler 22.03上安装配置drbd
- ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
- ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
- ¥15 Windows11, backspace, enter, space键失灵