Tuesday, May 23, 2017

How to validate params in REST Lumen/Laravel request?

Route: $app->get('/ip/{ip}', GeoIpController::class . '@show');

How to validate ip's properly? I've tried to inject Request object in show method, but wasn't able to solve this. I want to stick with REST, so using URL parameters is not solution for me. I use it for API purposes, so status code as response would be appropriate.

Also tried that way:

$app->bind('ip', function ($ip) {
    $this->validate($ip, [
        'ip' => 'required|ip',
    ]);
});

EDIT: The answer below is corret, what is more I've found in documentation:

Form requests are not supported by Lumen. If you would like to use form requests, you should use the full Laravel framework.

In other words, you cannot use custom requests via injection in constructors in Lumen.



via wujt

Advertisement