Laravel-参数未通过路由转发 [英] Laravel - Parameters are not forward by the route
本文介绍了Laravel-参数未通过路由转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
路线
Route::get('restaurants/@{latitude},{longitude},{radius}', 'RestaurantsController@nearbyRestaurants');
我一开始尝试做自己的请求,然后直接在控制器中做了一个验证器
I tried doing my own request at first, then did directly a validator in the controller
控制器的方法
public function nearbyRestaurants(Request $request)
{
$validator = \Validator::make($request->all(), [
'latitude' => 'digits_between:-90,90',
'longitude' => 'digits_between:-180,180',
'radius' => 'numeric'
]);
dd($validator);
}
dd(valisator)的结果
#initialRules: array:3 [▼
"latitude" => "digits_between:-90,90"
"longitude" => "digits_between:-180,180"
"radius" => "numeric"
]
#rules: array:3 [▼
"latitude" => array:1 [▼
0 => "digits_between:-90,90"
]
"longitude" => array:1 [▼
0 => "digits_between:-180,180"
]
"radius" => array:1 [▼
0 => "numeric"
]
]
解决方案
而不是:
public function nearbyRestaurants(Request $request)
{
}
使用
public function nearbyRestaurants($latitude, $longitude, $radius)
{
}
使用dd($ validator);我现在可以看到数据:
Using dd($validator); i can now see the data:
#data: array:3 [▼
0 => "59.93141200"
1 => "30.31992300"
2 => "15"
]
推荐答案
从URL获得的是控制器的参数,而不是验证器所需的全局$_GET
和$_POST
的参数. .您想要的是什么
What you're getting from the URL are arguments for the controller, not arguments from the $_GET
and $_POST
global, which is what the validator needs. What you want is this:
public function nearbyRestaurants(Request $request, $latitude, $longitude, $radius)
然后,手动创建一个验证器,并将其作为项目传递.
Then, manually create a validator and pass those in as items.
这篇关于Laravel-参数未通过路由转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文