dingo / api

A RESTful API package for the Laravel and Lumen frameworks.
BSD 3-Clause "New" or "Revised" License
9.33k stars 1.25k forks source link

how to add `message`, `code` to success response #1612

Closed toknT closed 5 years ago

toknT commented 5 years ago

How to add message, 'code' in success response ?

Digno return message, code when go error,

But how can I add they to a success response?

specialtactics commented 5 years ago

Hi

I actually do not recommend this kind of approach, because it creates redundancy with two sources of truth - 1) The HTTP response code 2) Your code response in the content of the response

You should ideally use the http response code for all situations.

However if you really want to do this, the best approach to take is to have a "BaseTransformer" which all your other transformers are extended from, and that you use by default, and in that base transformer, you just add that parameter to the response (so that it's there every time).

To do this you will probably need to pass it to the transformer from all your controllers, which is again why I think it is not a good approach.

toknT commented 5 years ago

@specialtactics thanks your replay, I found the solution

  1. add setSerializer to remove data key https://github.com/dingo/api/issues/1047#issuecomment-227355001 2 . set content in response event https://github.com/dingo/api/issues/1224#issuecomment-372601557

And really agree with your not add message and code in success response ,But it's our team 's requirement.

specialtactics commented 5 years ago

Great @toknT - good to hear !