所以,我必须像这样路由保护 - 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
。
任何指导赞赏
欣赏解说!!! –