如果我们使用Laravel 4创建一个简单的路由,我们可以使用where为每个参数设置一个正则表达式。传入URI。例如:
Route::get('users/{id}',function($id){
return $id;
})->where('id','\d+');对于每个get请求,第二个参数。必须是数字。当我们创建资源时,问题来了,如果我们使用->where(),它会抛出一个关于这不是一个对象的错误。
我试着把where放到一个组中,作为一个数组作为第三个参数。在资源中,但是没有worked.How,我们可以使用正则表达式的功能和Laravel4资源的功能吗?
发布于 2014-03-12 18:18:49
他的神像,
我被同样的问题弄疯了,所以我做了一些研究。我所能得出的结论是:你不需要这个方法。在每个控制器方法中,您可以在每个参数后指定默认值:
public function getPage(id = '0', name = 'John Doe'){接下来,您可以使用laravel验证器执行正则表达式检查,以及许多其他检查,如:
//put the passed arguments in an array
$data['id'] = $id;
$data['name'] = $name;
//set your validation rules
$rules = array(
"id" => "integer"
"name" => "alpha"
);
// makes a new validator instance with the data to be checked with the given
// rules
$validator = Validator::make($data, $rules);
// use the boolean method passes() to check if the data passes the validator
if($validator->passes()){
return "stuff";
}
// set a error response that shows the user what's going on or in case of
// debugging, a response that confirms your above averages awesomeness
return "failure the give a response Sire";
}因为大多数验证已经在laravel的options list中,所以您可能不需要正则表达式检查。然而,它是一个选项(regex:pattern)。
在控制器中不存在where()方法的背后,我的理论是该方法为您提供了在Route文件中进行验证的机会。既然你的控制器中已经有了这种可能性,那么就不需要它了。
https://stackoverflow.com/questions/16880742
复制相似问题