问题描述
我正在使用Laravel 5.1,并且具有以下代码:
I'm working on Laravel 5.1 and I have the following code:
//Routes.php
Route::get('/listagem/{campo}','ControllerParque@listar');
//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/tag'的所有其他内容.我的意思是,如果用户在导航器(URL栏)中输入"listagem/crap",则会被拒绝,因为它不是"listagem/usuario"或"listagem/tag".
I would like to restrict anything different of 'listagem/usuario' or 'listagem/tag'. I mean, if an user types in your navigator (URL bar) 'listagem/crap', it will be denied, because it's not 'listagem/usuario' or 'listagem/tag'.
我尝试过"where"子句":
I've tried 'where' clause':
Routes.php
Route::get('/listagem/{campo}','ControllerParque@listar')
->where('campo', 'usuario');
但是通过这种方式,我只限制了一个条件,这种情况是'listagem/usuario'.
But this way I'm restricting just the one condition, this case 'listagem/usuario'.
有什么主意吗?
推荐答案
您可以在"where"子句中使用正则表达式. usario|tag
仅匹配usario
或tag
http://laravel.com/docs/5.0/routing#route-parameters
You can use a regular expression in the "where" clause. usario|tag
would match only usario
or tag
http://laravel.com/docs/5.0/routing#route-parameters
Route::get('/listagem/{campo}','ControllerParque@listar')
->where('campo', 'usuario|tag');
这篇关于Laravel Route ::使用where条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!