首页 > Web开发 > 详细

thinkphp 响应对象

时间:2018-06-05 12:04:18      阅读:196      评论:0      收藏:0      [点我收藏+]
<?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‘
];
?>

 

thinkphp 响应对象

原文:https://www.cnblogs.com/lgxtry/p/9139042.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!