Dealing with Very Long Requests in Laravel

profile picture

Dealing with Very Long Requests in Laravel

Big headache problem that I encountered on some client's project, where I had to build a custom tool to import big ass excel files in the backoffice.

When the excel is being imported, we erase everything in the database and insert every entry again. When reaching 10 thousands entries, the load time of the important is very, very long.

And what happens when the execution time of your script takes too much time? Yes, it breaks, and return a Fatal Error. Even sometimes, it returns a 502 from Nginx.

So at first I tweaked the nginx configuration and the php configuration for that particular project. But now it's the entire application that is affected by this change of configuration.

It would be nice to only allow long request on specific routes, right ?

Well, we can do that!

Simply create a dead simple Middleware like this:


namespace App\Http\Middleware;

use Closure;

class AllowLongRequests
    public function handle($request, Closure $next)

        return $next($request);

And then simply bind this middleware into your Laravel app to any routes you want.

Prefer a Package ? I got you covered πŸ˜‰

I released a package that implements exactly this.

You can install this in your own app very easily:

Use Composer

composer require mydnic/laravel-allow-long-request

You can publish the config-file with:

php artisan vendor:publish --provider="Mydnic\AllowLongRequests\AllowLongRequestsServiceProvider" --tag="config"

In your Kernel.php you can register the middleware like this:

protected $routeMiddleware = [
    // ...
    'allow-long-request' => \Mydnic\AllowLongRequests\Http\Middleware\AllowLongRequests::class,

And now you can bind it your route in the Laravel way.

How does it work ?

Pretty simply actually!

Once you registered the middleware, every request going through it will modify the php configuration on the fly, but just for the time of the request.

The middleware is being processed before the controller, so you are sure that any code within your controller will be processed under the new php setting.

Hope you like it!


about me

profile picture

I consider myself as an IT Business Artisan. Or Consultant CTO. I'm a self-taught Web Developper, coach and teacher. My main work is helping and guiding digital startups.

more about me

follow me


A weekly email with the latests articles

support my work

Start trading on binance
  • BTC



  • ETH



  • Monero



2024 © My Dynamic Production SRL All rights Reserved.