Search code examples
laravellumenlaravel-validation

Laravel 5.5 best way to validate API endpoint parameters


Route

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

Controller action

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

My Requirement is not to throw an exception instead show a meaningful message to the user for all the following urls.

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

In other words, the variable year should be validated for numeric values, name, and category for alphabets.

N O T E: I am using Laravel Lumen

Any kind of help is appreciated


Solution

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

    2. Regular Expression Constraints + Custom exception Handler - https://lumen.laravel.com/docs/5.5/routing#parameters-regular-expression-constraints https://lumen.laravel.com/docs/5.2/errors#the-exception-handler

    I guess the first one is quite clear. The second one, you can catch your route exception, and render the view with a meaningful message.