本文介绍了Laravel Route ::使用where条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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仅匹配usariotag 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条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 08:50