创建前台控制器

后台控制器是专门给后台用的,只有加入后台管理员角色才能访问对应的接口,而前台控制器是对外开放的专门给前端程序对接的接口,比如APP/小程序/网站等。以文章分类为例,我们应该在actionphp/application/example/controller目录下建立Cate.php控制器文件,内容如下:

创建前台控制器 - 图1

  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | InitAdmin/actionphp [ InitAdmin渐进式模块化通用后台 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2018-2019 http://initadmin.net All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  9. * +----------------------------------------------------------------------
  10. * | Author: jry <ijry@qq.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace app\example\controller;
  14. use think\Db;
  15. use think\Request;
  16. use app\core\controller\common\Home;
  17. use \Firebase\JWT\JWT; //导入JWT
  18. /**
  19. * 文章分类控制器
  20. *
  21. * @author jry <ijry@qq.com>
  22. */
  23. class Cate extends Home
  24. {
  25. private $example_cate;
  26. protected function initialize()
  27. {
  28. parent::initialize();
  29. $this->example_cate = new \app\example\model\Cate();
  30. }
  31. /**
  32. * 获取分类信息
  33. *
  34. * @return \think\Response
  35. * @author jry <ijry@qq.com>
  36. */
  37. public function info($id)
  38. {
  39. $ret = $this->is_login();
  40. if($ret['code'] != 200){
  41. return json($ret);
  42. }
  43. $user_info = $this->example_cate
  44. ->field('id,title,status')
  45. ->find($if);
  46. return json(['code' => 200, 'msg' => '分类信息', 'data' => ['info' => $info]]);
  47. }
  48. }