何乔斯
我对同样的问题很生气,所以我做了一些研究。我只能得出以下结论:您不需要这种方法。在每个控制器方法中,您可以在每个参数之后指定默认值:
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 的选项列表中,您可能不需要正则表达式检查。然而它是一个选项(正则表达式:模式)。
我在控制器中不存在 where() 方法背后的理论是,该方法为您提供了在 Route 文件中进行验证的机会。由于您的控制器中已经有这种可能性,因此不需要它。