路线保护 - Laravel

时间:2015-12-30 05:10:15

标签: php laravel laravel-5 laravel-5.2

所以我有一个包含3个参数的路由

Route::get('search-restaurant/{location}/{day}/{time}', 'WebController@search_restaurant');

对于此路线的每个请求,我都想以某种方式验证这些参数。

对于time参数,我已经看到了如何将regex附加到5.2而不是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 } } 中的文档的文档,但即使我找到了我需要验证的文档其他人也是

所以基本上我已经尝试了两种不同的方法来检查和验证参数,但没有一种方法正常工作。

方法1 - 控制器

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);
}

方法2 - 中间件

if..else

正如您所看到的,我对变量做了简单的500 page语句,但条件似乎总是正确的。我也逐一尝试了这些规则,但每次失败都会被发送到<script type="text/javascript"> $(document).ready(function () { window.name = "parent"; $('#link').click(function (event){ event.preventDefault(); window.open('child.php', 'fullscreen=yes', 'scrollbars=auto'); }); }); </script> <body> <a id="link" href="/">Open Window </a> </body>

任何指导意见

2 个答案:

答案 0 :(得分:1)

首先,您可能想要回到条件语的基础知识。

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

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])',
]);

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

Here是5.1文档的链接,以备不时之需。

答案 1 :(得分:0)

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

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

它将永远为真,因此您将始终获得500错误页面。

现在,如果有人使用today作为$day,则(false || true)将评估为true,如果是tomorrow,则为||

您应该将此处的操作符从if($day != 'today' && $day != 'tomorrow') { // check string abort(500); } 更改为'&amp;&amp;':

in_array

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

var myApp = angular.module("myApp", []);

但还有一件事。您不应该在控制器或中间件中执行此操作。您可以像5.1中那样使用路由参数(https://laravel.com/docs/5.1/routing#route-parameters) - 我没有测试它但它应该可以工作或(建议这样做)您应该使用例如Form Validation Request来实现它。