这对其他人来说是显而易见的
我有一条有效的路线并转到正确的控制器
Route::get('v1/holidays/{country}/{year}/{month}/{official?}',
'retrieveHolidayController@test'
所以,如果我去 http://example.com/v1/holidays/US/2014/03/01
它会去我想去的地方
但是我希望链接看起来像
http://example.com/v1/holidays?country=US&year=2014&month=03&official=01
我该怎么办呢?
答案 0 :(得分:1)
您重新定义了到
的路线Route::get('v1/holidays', 'retrieveHolidayController@test');
然后在您的控制器中,您可以使用$ request
获取参数值public function test(Request $request)
{
if ( $request->has('country') && $request->country != '') {
$country = $request->country;
}
if ( $request->has('year') && $request->year != '') {
$year = $request->year;
}
.... // and the others. Then you can query like this
$holidays = Holiday::when($country, function($query) use ($country) {
return $query->where('country', $country);
})
->when($year, function($query) use ($year) {
return $query->where('year', $year);
})
->get();
//Using 'when' only executes the closure if the variable exists
}
现在,您可以按照自己的方式使用您的网址:
http://example.com/v1/holidays?country=US&year=2014&month=03&official=01
答案 1 :(得分:0)
让country,year and month
也可选: -
Route::get('v1/holidays/{country?}/{year?}/{month?}/{official?}', 'retrieveHolidayController@test')
答案 2 :(得分:0)
Route::get('v1/holidays', 'retrieveHolidayController@test');
Route::get('v1/holidays/{country}/{year}/{month}/{official?}', function($country){
return redirect()->to(action('retrieveHolidayController@test', ["country"=>$country,......]));
});
访问http://example.com/v1/holidays/US/2014/03/01
重定向到http://example.com/v1/holidays?country=US&year=2014&month=03&offical=01
如果官方param为null重定向参数offical=
param可以为空
isset($official){
$paramArry["official"] = $official;
}