thinkphp 響應對象response


1.可以通過修改配置文件的 default_return_type修改輸出類型

  // 默認輸出類型
    'default_return_type' => 'html',

2. 可以通過Config類設置輸出類型

<?php

namespace app\index\controller;

use http\Params;
use think\Config;
use think\Request;

import('filter', APP_PATH . 'common', ".php");

class Index
{
    /**
     * @param Request $request
     * @return string|void
     * @url http://localhost/news/5.html?name=jiang
     */
    public function index(Request $request)
    {
        $res = [
            'code' => 200,
            'result' => [
                'list' => [1, 2, 3, 4, 5, 6],
            ]
        ];
        /* //返回html類型
         return dump($res);*/

        /* 調整輸出類型為json */
        /*Config::set('default_return_type', 'json');
        return $res;*/
        /* 調整輸出類型為xml */
        Config::set('default_return_type', 'xml');
        return $res;
    }

}
<?php


namespace app\api\controller;


use think\Config;

class User
{
    public function  userInfo($type='json'){
        if(!in_array($type,['json','xml']))
            $type = 'json';
        Config::set('default_return_type',$type);
        $data= [
            'code' => 200,
            'reslut'=>['name'=>'jiang',
            'email' => '444@qq.com',],
        ];
        return $data;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM