1. cmd根目录执行代码创建控制器

      1. php think make:controller BaseController
    2. 写入代码 ```javascript namespace app\common\controller;

    use think\Controller; use think\Request;

    class BaseController extends Controller { static function showData($code=200,$msg=”无”,$data=[]){ $res=[ “code”=>$code, “msg”=>$msg, “data”=>$data ]; return json($res); } //没有数据的情况 static function showEmpty($code,$msg){ return self::showData($code,$msg); } }

    1. 3.修改index/index测试代码
    2. ```javascript
    3. namespace app\index\controller;
    4. use app\common\controller\BaseController;
    5. use app\common\validate\TestValidate;
    6. class Index extends BaseController
    7. {
    8. public function index()
    9. {
    10. (new TestValidate())->goCheck('login');
    11. }
    12. public function testController(){
    13. $list=[
    14. ['id'=>10,"title"=>"测试","content"=>"内容"],
    15. ['id'=>2,"title"=>"测试2","content"=>"内容2"]
    16. ];
    17. // return self::showData(200,"嘿嘿",$list);
    18. return self::showEmpty(200,'没有数据');
    19. }
    20. }
    1. 查看结果
      1. {
      2. "code":200,
      3. "msg":"嘿嘿",
      4. "data":[
      5. {"id":10,"title":"测试","content":"内容"},
      6. {"id":2,"title":"测试2","content":"内容2"}
      7. ]
      8. }