Very good question, trying to come with solution. Anyone has tried this already?
How can I use Illuminate\Foundation\Http\FormRequest on Lumen Micro Framework
I like to use the new FormRequest metodologie to validate requests in L5. How can I use it in Lumen?
I wanna know as well as what would be an easy way to have this feature on Lumen
I'm not sure if either of you figured this one out, but I stumbled upon this thread with the same problem. Here's how to fix it for lumen:
-
Copy FormRequest.php from a laravel project into your requests folder. You can find it in: vendor\laravel\framework\src\Illuminate\Foundation\Http\FormRequest.php
-
Change the namespace in FormRequest.php to "namespace App\Http\Requests;"
-
Edit requests.php to extend the file you just added (i.e change the namespace to the one you just made) to:
<?php
namespace App\Http\Requests;
//use Illuminate\Foundation\Http\FormRequest; --old
use App\Http\Requests\FormRequest;
abstract class Request extends FormRequest
{
//
}
-
You will also need to copy the FormRequestServiceProvider into your App\Providers folder and register it in bootstrap/app.php
-
You will need to add $app->make('validator'); in app.php (after $app->withEloquent(); should be fine). Also add the following in your "Register Service Providers":
$app->bind(
'Symfony\Component\Translation\TranslatorInterface',
function ($app) {
return $app['translator'];
}
);
$app->register(App\Providers\FormRequestServiceProvider::class);
- All fixed :) Lumen should already include all the required packages needed for FormRequest, so you shouldn't have to update anything. Now you can extend the Request in Lumen the same way you do in Laravel.
In the end your directory structure should be similar to Laravel:
-app
-Http
-Requests
Request.php
FormRequest.php
AnotherRequest.php
etc...
@allybeaton, nice!
Trying to get it running, unfortunately the $this->container is not initialised in the FormRequest class:
PHP Fatal error: Call to a member function make() on a non-object in /usr/local/www/myapp/api/app/Http/Requests/FormRequest.php on line 75
(Copied from Laravel 5.0)
Have you had the same issue and if so, how did you solve it?
Edit
I found that the event router.matched is never fired, so the FormRequest doesn't get initialised in the FormRequestServiceProvider boot method properly. (Not yet found the reason for that though.)
Cheers, Rob
Really a big work to only get Form Request, why not just use validate method in controller ?
I've just created my own little clean implementation by doing:
Add this in your controller function: $this->validateRequest(new CreateUserRequest, $request);
This in your own base controller:
public function validateRequest(FormRequest $formRequest, Request $request)
{
parent::validate($request, $formRequest->rules());
}
And then just have a class like CreateUserRequest that has a rules method which extends from FormRequest which is just an interface.
To make authorization work you could just have a check like:
public function validateRequest(FormRequest $formRequest, Request $request)
{
if (!$formRequest->authorize()) throw new UnauthorizedException;
parent::validate($request, $formRequest->rules());
}
Thanks for manual. Created library based on michaelbonds/lumen-make and your info. https://packagist.org/packages/smskin/lumen-make Maybe someone will need.
Did the same as explained, however, getting >> Call to a member function make() on null
@robs , could you please let us know what did you do to fix the issues related to container object not initialized? Thanks
Fixed the error : instead of normal Laravel file path for redirector, use >> use Laravel\Lumen\Http\Redirector; in FormRequestServiceProvider, And FormRequest.
Basically Http Redirector isn't necessary in FormRequest, so you could completely remove it from FormRequest. Because normally Lumen used to build an API, so the validation response must be thrown in the same page (using JsonResponse), not in the previous page (or custom page) like in Laravel.
There is a lot of mistakes in that article
Please or to participate in this conversation.