cmd根目录执行代码创建控制器
php think make:controller BaseController
写入代码 ```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); } }
3.修改index/index测试代码```javascriptnamespace app\index\controller;use app\common\controller\BaseController;use app\common\validate\TestValidate;class Index extends BaseController{public function index(){(new TestValidate())->goCheck('login');}public function testController(){$list=[['id'=>10,"title"=>"测试","content"=>"内容"],['id'=>2,"title"=>"测试2","content"=>"内容2"]];// return self::showData(200,"嘿嘿",$list);return self::showEmpty(200,'没有数据');}}
- 查看结果
{"code":200,"msg":"嘿嘿","data":[{"id":10,"title":"测试","content":"内容"},{"id":2,"title":"测试2","content":"内容2"}]}
