3

When I develop a Laravel application I used to clear cache after making any changes to routes\web.php or routes\api.php. Recently I was working on a project for a fellow and found out that the project does not need clearing cache every time I make a change in any of the files I have mentioned.

So I want to know what is the problem with the autoloader or what exactly is the general problem?

Cosmin Staicu
  • 1,809
  • 2
  • 20
  • 27
  • I keep seeing people recommend "please clear your cache with cache:clear" as solution in both comments and answers in this site. It may cause lots of catastrophic results in your production environment that you can't recover. Clearing cache(cache:clear) has nothing to do with route files. Instead you may use/need route:cache (which also invokes route:clear) to refresh cached application routes. You may also check this answer for some insight (https://stackoverflow.com/a/61879432/2188922) – Ersoy Jul 19 '20 at 12:16
  • Did you check if the routes were cached in the first place? Or maybe it included routes with inline functions that cannot be cached? No caching, no need to clear the cache. – Maarten Veerman Jul 19 '20 at 12:38
  • Thank you @Ersoy. I got your point but my question is why should I clear cache everytime i change in routes files, though there is some environment I might not do so. Is there is any problem with my server environment..? – Abdullah Muhammed Jul 19 '20 at 13:46
  • Actually you don’t have to, clearing cache has nothing to do with route files. Route component of the framework has different commands to invalidate caches routes such as route:clear or route:cache. – Ersoy Jul 19 '20 at 13:48
  • @MaartenVeerman I listed my routes before and after changes. and routes table not changed unless I clear the cache – Abdullah Muhammed Jul 19 '20 at 13:51
  • @Ersoy Yes i meant route:clear :D – Abdullah Muhammed Jul 19 '20 at 13:54

1 Answers1

5

When you use commands like

php artisan optimize

php artisan route:cache

your route files(under routes/) are being parsed and cached. Now, next requests will be routed from cached routes, not from routes/*.php.

If you have used above commands, after making changes to route php files, you should re-cache them, or use

php artisan route:clear

to remove the cache. Then, next requests will be routed by routes/*php files.

Davud Safarov
  • 498
  • 4
  • 12