账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    laravel+dingo/api书写API接口的返回体中code和message是怎样配置的?
    35
    0

    如下是我写的API接口的controller:

    <?php
    
    namespace App\Http\Controllers\Lucky;
    
    use Illuminate\Http\Request;
    use App\Http\Controllers\BaseController as BaseController;
    use App\Models\Card;
    use App\Http\Transformers\CardsTransformer;
    
    class CardsController extends BaseController
    {
        public function test(Request $request) {
            $unionId = $request->input('unionId');
            $card = new Card;
            $card->union_id = $unionId;
            $card->fi_card = 1;
            $card->s_card = 2;
            $card->t_card = 3;
            $card->fo_card = 4;
            $card->save();
            return $this->response->item($card, new CardsTransformer());
        }
    }

    这样写,调用该接口得到的返回体如下:

    {
        "data": {
            "unionId": "test",
            "fiCard": 1,
            "sCard": 2,
            "tCard": 3,
            "foCard": 4
        }
    }

    可是我认为正确的API接口的返回体应该如下:

    {
        code:0,
        message:"ok",
        data:{},
        useTime:0.017374038696289062
    }

    请问在laravel和dingo/api的技术栈中,怎么去为每个接口的返回体配置codemessage字段?及其相应的内容?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答