I know this is might sound a little stupid but here I go.
Im currently building a API using Lumen and i have a number of calls that need to be authenticated this i can handle fine as I can use a middleware to handle the auth and reject non authenticated calls.
some of the calls also have other things that need to be applied to them i.e. a user can only hit a certain end point with a certain id twice a day and no more my question is for this type of call would it make sense to use a middleware to check the user details and perform the logic there so that it never gets through to the controller if they are trying to hit the end point more than twice in one day, also again some endpoints can only be hit i.e. the user can only start a battle on a property they dont own so I want to check in a middleware if they own the property if so just return some json letting them know this.
I really like how the form request works in laravel 5 and how failed validation never gets to the controller method, so does doing this kind of thing in a middleware make sense or am i using it for the wrong type of thing and I should perform all validation inside my controller?
thanks @pmall can we actually use FormRequests for API calls as i thought FormRequests returned the user to the previous route on fail. I've just taken another look at the docs and I see that lumen has a ValidatesRequest traits that I can use in controllers my only issue is that when this fails it throws a exception which is automatically caught and then redirects a user to the previous location, which again wouldnt really work.
ive just tried to use the ValidatesRequests trait in a controller but im sending the API call with a content-type of application/json and the body of the call contains a json object, ive deliberately made it fail validation to see if it returns a response but it tried to redirect me to the '/' route but as i dont have that route in my routes file its then throwing a 404 error as it doesnt exist.
in the docs it says that it only returns JSON if im making a Ajax call but im not actually making a Ajax call, so im a little stumped I either do the validation in the controller not using the validation trait but using the old fashioned Validator::make method then handle it all in the controller or continue to use various middleware to handle the validation.
ive just tried to use the ValidatesRequests trait in a controller but im sending the API call with a content-type of application/json and the body of the call contains a json object, ive deliberately made it fail validation to see if it returns a response but it tried to redirect me to the '/' route but as i dont have that route in my routes file its then throwing a 404 error as it doesnt exist.
Must be something wrong with your request. Here the method that generate the response of the form request :
# Illuminate\Foundation\Http\FormRequest
/**
* Get the proper failed validation response for the request.
*
* @param array $errors
* @return \Symfony\Component\HttpFoundation\Response
*/
public function response(array $errors)
{
if ($this->ajax() || $this->wantsJson())
{
return new JsonResponse($errors, 422);
}
return $this->redirector->to($this->getRedirectUrl())
->withInput($this->except($this->dontFlash))
->withErrors($errors, $this->errorBag);
}
I can certainly see that in L5 but ive just checked and I dont believe lumen has form request support I could be wrong? it does have route controller validation though.
you are right @dannewns Lumen doesn't have form request but it should be possible to add this to lumen however I don't know exactly how it would be possible, but maybe someone could help us.