验证API端点Laravel 5.5最好的方法参数

路线验证API端点Laravel 5.5最好的方法参数

Route::get('attendence/{year?}/{name?}/{category?}, '[email protected]'); 

控制器动作

function users($year=null, $name=null, $category= null){ 

}

我的要求是不是抛出一个异常,而不是显示出有意义的信息给用户的所有以下网址。

http://localhost:8080/2012/john/travel 

http://localhost:8080/undefined/john/travel

http://localhost:8080/undefined/john

http://localhost:8080/john

换句话说,变量今年应该被确认为数值,名称和类别字母。

注: 我使用Laravel流明

任何形式的帮助表示赞赏

回答:

  1. 验证 - https://lumen.laravel.com/docs/5.5/validation

  2. 正则表达式约束+定义异常处理程序 - https://lumen.laravel.com/docs/5.5/routing#parameters-regular-expression-constraints https://lumen.laravel.com/docs/5.2/errors#the-exception-handler

我想第一个很清楚。第二个,你可以捕捉你的路线异常,并用一个有意义的消息渲染视图。

以上是 验证API端点Laravel 5.5最好的方法参数 的全部内容, 来源链接: utcz.com/qa/258531.html

回到顶部