Interface return data generally the shape and floating point type as a string output, you have to deal with this type?
What to do with the current laravel frame.
My idea is to define the data type on each model side, and then handle the model output uniformly, what's the best way to do it.
Reply content:
Interface return data generally the shape and floating point type as a string output, you have to deal with this type?
What to do with the current laravel frame.
My idea is to define the data type on each model side, and then handle the model output uniformly, what's the best way to do it.
PHP is a weakly typed language, so this situation is more common.
Generally, if you interact with JS, or other weakly typed languages, there is no problem.
If you have a strong language like Ios or Android, there are some problems
1, write a unified API processing function, in the output before the unified processing, is an int with intval to turn, is null is NULL, is an empty string, is "", this in the strong type needs attention.
2, interactive side, Ios, Android, etc., it is best to do type verification, you can do type conversion, do fault tolerance, do not return the wrong type of interface, the program will be collapsed.
It is best to deal with both sides.
This is not related to the framework, PHP is a weak type, so the data format is very irregular when returning JSON, I currently return the JSON data interface is on the outermost hand format
strval(xxx) //格式化为字符串intval(xxx) //格式化为整形数字floatval(xxx) //格式化为浮点型数字boolval(xxx) //格式化为浮点型
Thanks for the invitation, the upstairs is very clear, the PHP weak type, you can according to the data you want to accept, or return to your client's data alone with the program to do the processing, to ensure the integrity of the data accuracy. You can set a class or method to deal with the floating point, integer type and other data, in short, ensure that the interface security data correctness is good
First of all, say my results.
is not converted to a string.
is not your data is originally a string.
My PHP Version: 5.5.34
Laravel Framework version:5.2.22
顺便一提,我认为处理的时候应该是在controller返回的时候去统一处理。