thinkphp 响应对象

<?php
namespace app\admin\controller;
use think\Request;
class Index{
    public function index(Request $request){
        $res=[
            'code'=>200,
            'result'=>[
                'list'=>[1,2,3,4,5]
            ]
        ];
        return var_dump($res);
    }
}
?>

页面返回内容,用return

return '123';代替echo '123';返回字符串

返回类型的控制

thinkphp 下 convention.php  'default_return_type'控制,默认输出类型

使用动态配置,返回json格式

<?php
namespace app\admin\controller;
use think\Request;
use think\Config;
class Index{
    public function index(Request $request){
        $res=[
            'code'=>200,
            'result'=>[
                'list'=>[1,2,3,4,5]
            ]
        ];
        Config::set('default_return_type','json');
        return $res;
    }
}
?>

可以新建api模块,设置默认返回类型为json;

conf/api/config.php

<?php
return [
'default_return_type'=>'json'
];
?>

猜你喜欢

转载自www.cnblogs.com/lgxtry/p/9139042.html