1

所以我有一条带有 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.

任何指导表示赞赏

4

2 回答 2

1

首先,您可能想回到条件句的基础。

如果您需要验证 3 个参数,则需要执行 3if

if ($param1 === $validLocation) {}
if ($param2 === $validDay) {}
if ($param3 === $validTime) {}

条件的工作方式if...elseif...else是,一旦满足第一个条件,将不再检查其余条件。

// if this condition is true, PHP will not check for further `elseif` or `else
if($day != 'today' || $day != 'tomorrow') {
    abort(500);
} elseif (!in_array($location, $locations)) {
    abort(500);
} else {
    //rest of code - send to view
}

我很抱歉跑题了,但是是的,在 5.2 文档中,regex可能已被删除或移动到其他地方,但您仍然可以在 5.1 中找到这些文档

不过,我建议您在路由中使用约束,而不是在控制器或中间件中检查它。

Route::get('test/{location}/{day}/{time}', function ($location, $day, $time) {
    dd($location, $day, $time);
})->where([
    'location' => 'central|garki-1|garki-2|wuse-2|wuse-1|gwarimpa|maitama|asokoro',
    'day' => 'today|tomorrow',
    'time' => 'asap|(2[0-3]|[01][0-9])([0-5][0-9])',
]);

上述路由将在将所有参数传递给Closureor之前检查所有参数的正则表达式Controller@action(根据需要进行修改)

如果您需要,这里是 5.1 文档的链接。

于 2015-12-30T07:06:18.797 回答
0

我已经注意到的第一个问题是以下情况:

if($day != 'today' || $day != 'tomorrow') { // check string
    abort(500);
}

这将永远是真的,所以你总是会得到 500 错误页面。

现在,如果有人使用todayas $dayit will be 如果(false || true)它评估为true,则相同,如果它是tomorrow

您应该将此处的运算符从||'&&' 更改为:

if($day != 'today' && $day != 'tomorrow') { // check string
    abort(500);
}

in_array在这里使用

if(!in_array($day, ['today','tomorrow'])) { // check string
    abort(500);
}

但还有一件事。你不应该在你的控制器或中间件中这样做。您可以使用 5.1 中的路由参数(https://laravel.com/docs/5.1/routing#route-parameters) - 我尚未对其进行测试,但它应该可以工作,或者(推荐)您应该使用例如Form验证请求来实现这一点。

于 2015-12-30T07:03:55.963 回答