I have a URL of the following form:
GET /cat/{cat}/meows/{overTime}
I can write a controller method that looks like this in order to capture the values:
public function getMeowsOverTime(Cat cat, int overTime)
What I would like is to validate the {cat}
and {overTime}
values before it gets to the controller method.
I have tried creating a custom MeowsOverTimeRequest
object that extends Illuminate\Http\Request
according to the recommendation here: Laravel 5.7 - Override all() method in Request validation Class to validate route parameters?
So that would be:
class MeowsOverTimeRequest extends Request
{
public function authorize()
{
return true;
}
public function all($keys = null)
{
$request = parent::all($keys);
$request['cat'] = $this->route('cat');
$request['overTime'] = $this->route('overTime');
return $request;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'cat' =>
[
'required',
'integer'
],
'overTime' =>
[
'required',
'integer',
Rule::in(Cat::getMeowTimeOptions())
]
];
}
}
But $this->route('cat')
and $this->route('overTime')
both return null. How can I access these variables?
I would prefer not to use regular expression validation due to the overTime
being limited by what is returned from Cat::getMeowTimeOptions