How to bring back ‘Form Request’ to Lumen 5.x

Lumen is an excellent micro-framework to do API-based projects. I have been doing multiple Lumen projects this year. Both clients and developers love it as it has the flexibility to work on REST API. If you are not familiar with Lumen already, it is the strip down version of Laravel which is built with speed as its target. 
Lumen contains most of the features found in Laravel, but in addition it also removes some features such as Session Driver which helps the framework to boost the speed. Lumen installation does not include sessions due to the fact that tokens are mostly used in an API based backend instead of sessions.


Most of the time I have everything I need to deliver a fast micro service using Lumen, chances are I always end up bringing some functionality that I love from Laravel back to Lumen. Today I will be writing about how I end up bringing Form Request feature to Lumen 5.3. Form requests are custom request classes that contain validation logic for more complex validation scenarios . So let’s dive into coding!

First, let’s create a Request.php file under App\Http\Requests folder. If Requests folder doesn’t exist, you go ahead and create a new one. Make sure to copy paste the following code.


Now that we have the backbone of Form Request’s class we will be creating a service provider so that our Lumen app knows and register it to our application to be used by Controllers. To achieve this we have to create FormRequestServiceProvider.php file inside App\providers folder then copy paste the following code

You can see from the above code we are resolving our Request class that we created earlier and initializing Request in our application when needed. We are almost completing this blog; the only thing left is to register this service provide so that everything we create can be use by our application. To do this, we have to edit our app.php file that can be found under bootstrap folder. we have to add this line below where it says Register service provider and save the file.

$app->register(App\Providers\FormRequestServiceProvider::class);

Now we have a working Form Request class feature inside Lumen. So let’s test it. Go to app\Requests and create a file called TestRequest.php and add the following code

Now that we have a Request class, we can inject it to Our controller as if we were in Laravel. The above code works fine but, we can’t run the beautiful artisan command php artisan make:request

To add the above command and more to Lumen, you can use smskin’s package lumen-make from https://github.com/smskin/lumen-make .

It was all about, how to bring back ‘Form Request” feature from Laravel in to Lumen. If you faced any problem tell me below by comment, feel free to say hi. I’m waiting for your suggestion.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.