In my Laravel 5.1 application, I have a controller that serves as a RESTful API. Many methods of this controller have a following piece of code:
foreach (['email', 'password', 'firstName', 'lastName'] as $parameter) {
if (!$this->request->has($parameter)) {
throw new InputException(400, $parameter, 'Missing ' . $parameter . ' parameter.');
}
}
I know it's not the greatest idea to repeat the same piece of code (with different array contents). Instead, I'm looking for some elegant way to achieve the same thing.
I thought about middleware with parameters, something like this in the routes.php:
'middleware' => 'parameters:email,password,firstName,lastName'
Currently I'm using implicit controllers though, so my methods are not mentioned in routes.php
file.
What is the most flexible and elegant way to implement this functionality without repeating myself?