所以我有一条带有 3 个参数的路线,就像这样
Route::get('search-restaurant/{location}/{day}/{time}', 'WebController@search_restaurant');
对于此路由的每个请求,我想以某种方式验证这些参数。
对于time
参数,我已经看到了有关如何附加 a 的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
.
任何指导表示赞赏