Laravel 5.5 验证 API 端点参数的最佳方式

Laravel 5.5 best way to validate API endpoint parameters

路线

Route::get('attendence/{year?}/{name?}/{category?}, 'EmployeesController@users');

控制器动作

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

换句话说,变量 year 应该针对字母的数值、名称和类别进行验证。

备注: 我正在使用 Laravel Lumen

感谢任何形式的帮助

  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

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