2015-12-30 122 views
1

所以,我必須像這樣路由保護 - 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

任何指導讚賞

回答

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

上述路線將檢查正則表達式中的所有參數將它傳遞給Closure[email protected]之前(根據需要進行修改)

Here是鏈接到5.1文檔的情況下,你需要它。

+0

欣賞解說!!! –

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

或使用in_array這裏

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

但有一兩件事。你應該不要在你的控制器或中間件中執行它。您可以像5.1(https://laravel.com/docs/5.1/routing#route-parameters)那樣使用用戶路由參數 - 我沒有測試過它,但它應該可以工作,或者(這是推薦的),您應該使用例如Form Validation Request來完成此操作。