版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hjh15827475896/article/details/85110634
今天使用tp5 做参数验证是 (正整数时)发现一个问题
当路由中定义了参数时,发现tp5会自动把小数位数去掉
如
Rotue::get("api/:verison/themeOne/:id","api/v1.theme/getThemeOne");
访问地址 :http://myshop.test/api/v1/themeOne/22.4
此时 在getThemeOne方法中接收到的 $id 为 22 (居然自动取整了)
换一种方试定义路由
Route::get("api/:version/themeOne","api/v1.theme/getThemeOne") ; 这里没有在路由中指定变量id
访问地址同样是 : http://myshop.test/api/v1/themeOne/22.4
此时 在方法中 接收到的 $id 就是 22.4
记录一下这个坑,以防后面踩到
另记录一下 判断 传入的参数是一个 正整数的方法
//is_numeric 来判断传入的是不是一个数值型的
//intval($value)."" === $value 把value取整后再变在一个字串,全等于 $value;
// $value > 0 不用说了
if(is_numeric($value) && (intval($value)."" === $value) && $value > 0){
return true;
}else{
return false;
}