【问题标题】:Laravel 5.5 best way to validate API endpoint parametersLaravel 5.5 验证 API 端点参数的最佳方法
【发布时间】:2018-05-16 17:18:05
【问题描述】:

路线

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

控制器动作

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

我的要求是不要抛出异常,而是向用户显示以下所有 url 的有意义的消息。

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

换句话说,应该验证变量年份的数值、名称和字母类别。

注意: 我正在使用 Laravel Lumen

感谢任何形式的帮助

【问题讨论】:

    标签: laravel lumen laravel-validation


    【解决方案1】:
    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

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

    【讨论】:

    • 你到底是什么意思?请将您的代码添加到问题中
    猜你喜欢
    • 2011-06-03
    • 2019-08-06
    • 2018-09-13
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 2015-04-23
    相关资源
    最近更新 更多