一、Code样例

  1. <?php
  2. namespace App\Module\Cms\Controller;
  3. use Swork\Server\Http\Argument;
  4. use ......
  5. /**
  6. * 资讯内容控制器
  7. * @Controller("/cms/info")
  8. */
  9. class InfoController extends BeanCollector
  10. {
  11. /**
  12. * @Inject()
  13. * @var InfoLogic
  14. */
  15. private $infoLogic;
  16. /**
  17. * 保存资讯内容数据
  18. * @param Argument $argument
  19. * @Validate(Method::Post)
  20. * @return array
  21. * @throws
  22. */
  23. public function save(Argument $argument)
  24. {
  25. //外部参数
  26. $cid = $argument->post('cid', 0); //分类ID
  27. $title = $argument->post('title', ''); //资讯标题
  28. $author = $argument->post('author', ''); //作者
  29. $content = $argument->post('content', ''); //资讯详细内容
  30. //保存数据
  31. $this->infoLogic->save($cid, $title, $author, $content);
  32. //返回
  33. return 'success';
  34. }
  35. }

二、解析说明

1)类名注释为XX业务块的控制器
2)方法名save注释为保存XX业务块的数据
3)三段式注释:外部参数、保存数据、返回;
4)无特别要求,通常统一返回字符串 success 。
5)强制使用POST的方式接收数据。
6)在参数小于等于6个时,按单个参数单独传入;超过时,统一使用$others中传中。
即传入参数量最多为6个。

二、save、create、modify区别

方法名 描述说明
save 在前端(UI)既提供给新增,又提交给修改的接口
在后面逻辑层,在一个方法里面,通过主键(关键数据)区分是否新增 或 修改的操作
create 仅提供对应业务操作是创建某个业务内容,如新增资讯内容
modify 仅提供对应业务操作是更新某个字段内容,如修改资讯内容

四、超多字段样例

  1. <?php
  2. public function save(Argument $argument)
  3. {
  4. //外部参数
  5. $cid = $argument->post('cid', 0); //分类ID
  6. $title = $argument->post('title', ''); //资讯标题
  7. $author = $argument->post('author', ''); //作者
  8. $content = $argument->post('content', ''); //资讯详细内容
  9. $subdesc = $argument->post('subdes', ''); //概要描述内容
  10. //其它参数
  11. $others = [
  12. 'imgsrc' = $argument->post('imgsrc', ''); //封面图片
  13. 'pubdate' = $argument->post('pubdate', ''); //发布日期
  14. 'pubsrc' = $argument->post('pubsrc', ''); //发布来源
  15. ];
  16. //保存数据
  17. $this->infoLogic->save($cid, $title, $author, $content, $subdesc, $others);
  18. //返回
  19. return 'success';
  20. }