NeilSiao's avatar

Route is too many. more than 4000. help...

laravel version: 5.8.27.

PHP: ^7.3

Hello, In our project we have too many routes which cause performance issue and memory issue. We use Route::getRoutes(); to find out how many route do we have and the result "allroutes" shows 4498 routes. and in the future it might grows to 5000 routes. And then we use barryvdh/laravel-debugbar to see how is the different memory will be used when we have so many routes Loaded.

Result: around 200 Routes : total memory usage is 3MB around 4498 Routes: total memory usage is 8MB

We Have 5 MB Used In Routes!!

So the problem is how should we deal with it. Currently I only have few ways to try.

  1. Upgrade to laravel ^7.x Since Symfony's Router have a huge improvement. and Laravel integrate it in version 7.x
  2. Use PHP 7.4 preload Feature. to load hot classes in to memory.
  3. Don't load every route in every request. only load it when we need it. (This one i guess need to change a lot of code.)

I don't know. Is there any options better than my thoughts. If you can tell me. I'm REALLY Appreciate it.

0 likes
3 replies
NeilSiao's avatar

Thank you for your feedback, I watched the link content, but it looks like only showing how to organized route file. and in our project we do have 64 different route.php with different prefix, and register it in Route service provider. But the problem is 64 route.php will be registerd when a request come in, and It cost a lot of memory , And when web server have many request under web pressure testing we will see lot of out of memory error.

I think we can add some if statement to register what url prefix correspond to what route.php then register it. but this method could cause some problem if we use route('name') but the route.php did't register, then we need to rewrite our project. If you have other idea please tell me, thanks again.

jlrdw's avatar

I wish at times laravel had a generic front controller for certain request that may not need a route. I built that into a custom framework, but I wouldn't know how in laravel, probably not possible.

But you could, in some instances pass some data via calling a method to bypass some routes. A little hacky, but could work.

Please or to participate in this conversation.