2015-12-21 21 views
0

我工作的Laravel 5.1和我有以下代碼:Laravel路線::得到利用那裏的條件

//Routes.php 
Route::get('/listagem/{campo}','[email protected]'); 

//ControllerParque.php 
    public function listar($campo) {  
     $listagem = DB::SELECT("SELECT COUNT(x.".$campo.") AS nreg, x.* FROM (SELECT * FROM computadores ORDER BY id DESC) AS x GROUP BY ".$campo." ORDER BY ".$campo." ASC"); 
     $dados = array('listagem' => $listagem, 'campo' => $campo); 
     return view("ViewListagem")->with($dados);   
    } 

我想任何限制的「listagem/usuario」或「listagem /標籤不同」。我的意思是,如果用戶輸入您的導航器(URL欄)'listagem/crap',它將被拒絕,因爲它不是'listagem/usuario'或'listagem/tag'。

我試過「其中」條款':

Routes.php 
    Route::get('/listagem/{campo}','[email protected]') 
->where('campo', 'usuario'); 

可是這樣一來,我只是限制了一個條件,這種情況下‘listagem/usuario’。

有什麼想法?

回答

0

我不確定是否可以通過哪裏完成。相反,您可以在控制器功能中檢查它。

public function listar($campo) { 
    if(!in_array($campo,['usuario','tag'])) { 
      // throw an error, return to previous page 
      return Redirect::back()->withErrors('error message here'); 
    } 
    // Continue on with your function 
}