Route Caching in Laravel

Quick and painless way to get up to 100x faster route registration in your application.



Introduction

I was a non believer too when I heard about route caching and how it will improve my application loading speed. You are probably reading this lesson because you want a quick and painless way to gain speed improvements for your application without it being a pain in the ass.

You are lucky. Route caching is as simple as entering one command and the benefits are awesome. There is a small chapter on route caching in the Laravel 5.3 documentation, so be sure to give it a read. It is very small.

In this lesson I will explain what route caching is, how it works and how to use it.

What it is

Route caching is a mechanism which caches your routes file, so that that cached file will be loaded on every request instead of scanning the actual routes file and registering all of your application's routes.

"In some cases, your route registration may even be up to 100x faster." - source

How it works

Once you run the command, it creates a routes.php file in bootstrap/cache directory. If you open that file, you should see a comment:

"Here we will decode and unserialize the RouteCollection instance that holds all of the route information for an application. This allows us to instantaneously load the entire route map into the router." - comment from bootstrap/cache/routes.php.

I will not paste the content of the file here because it can be quite huge and it is mostly unreadable to the human eye. This is the gist of that file and what it contains:

<?php

app('router')->setRoutes(
    unserialize(base64_decode('...gibberish...'))
);

So basically, route caching loads this file directly into your application on every request instead of scanning the actual routes file and creating a RouteCollection. This is where the speed benefit comes from.

How to use it

Now the tricky part. From your project root, in the command line enter:

php artisan route:cache

And it's done! Simple as that.

If for some reason you want to get rid of route caching, you can run php artisan route:clear or manually delete the file bootstrap/cache/routes.php.

Remember to generate a fresh route cache every time you add any new routes.

Deployments

If you are using a continuous integration or a continuous deployment service like Codeship or Envoyer be sure to add php artisan route:cache to your deployment commands. This will save you a lot of headaches, trust me.

Troubleshooting

There are two things that could go wrong:

  • you forgot to run php artisan route:cache when you changed your routes file
  • you have closure based routes in your routes file

When in doubt, always run php artisan route:cache or php artisan route:clear, depending on what you are trying to do.

There is one limitation of route caching that I did not mention until now. In order to be able to use route caching, you must convert any closure based routes to controller classes. Basically, no closure routes allowed.

Thank you for reading and I hope that you are using route caching by now. Press the like button if you like this lesson and/or leave me a comment bellow.

Credits

Comments