所以,我必須像這樣路由保護 - Laravel
Route::get('search-restaurant/{location}/{day}/{time}', '[email protected]_restaurant');
與3個參數的路線的每個請求這條路線,我想驗證以某種方式或其他這些參數。
對於time
參數我已經看到了如何連接一個regex
它,但沒有5.2
但文檔文件,即使我找到的文檔,我需要覈實其他人也
所以基本上我已經嘗試了兩種不同的方法來檢查和驗證參數,但沒有任何工作。
方法1 - 電腦板
public function search_restaurant ($location, $day, $time) {
if($day != 'today' || $day != 'tomorrow') {
abort(500);
} elseif (!in_array($location, $locations)) {
abort(500);
} elseif (!preg_match("/(2[0-3]|[01][0-9])([0-5][0-9])/", $time) && $time != "asap") {
abort(500);
} elseif ($day == "tomorrow" && $time == "asap") {
abort(500);
} else {
.....//rest of code - send to view
}
}
方法2 - 中間件
public function handle($request, Closure $next)
{
$location = $request->route('location');
$day = $request->route('day');
$time = $request->route('time');
$locations = Array('central','garki-1','garki-2','wuse-2','wuse-1','gwarimpa','maitama','asokoro');
if($day != 'today' || $day != 'tomorrow') { // check string
abort(500);
} elseif (!in_array($location, $locations)) { // check against array
abort(500);
} elseif (!preg_match("/(2[0-3]|[01][0-9])([0-5][0-9])/", $time) && $time != "asap") { // check agains regex
abort(500);
} elseif ($day == "tomorrow" && $time == "asap") { // check against string
abort(500);
}
return $next($request);
}
正如你可以看到我做簡單的變量簡單if..else
聲明,但條件似乎永遠是真的。我也一一嘗試了這些規則,但每次失敗時我都會發送到500 page
。
任何指導讚賞
欣賞解說!!! –