php

How To Properly Do Route Caching

If you are looking to get the most out of your Laravel application load time, you might want to take a look at route caching. One of the pieces of Laravel’s bootstrap that can take anywhere from a dozen to a few hundred milliseconds is parsing the routes files, and route caching speeds up this process significantly.

To cache your routes file, you need to be using all controller and resource routes (no route closures). If your app is not using any route closures, you can run php artisan route:cache Laravel will serialize the results of your routes files. If you want to delete the cache, run php artisan route:clear.

Minor Drawback

If you do decide to take me up on this, then be aware that Laravel will now match routes against that cached file instead of your actual routes files. You can make endless changes to those files and they won’t take effect until you run route:cache again. This means you will have to recache every time you make a change, which introduces a lot of potential for confusion.

Advice

It is recommended that you only cache in production. Since Git ignores the route cache file by default, consider only using route caching on your production server, and run the php artisan route:cache command every time you deploy new code (whether via a Git post-deploy, a Forge deploy command, or as a part of whatever other deploy system you use). This way you won’t have the confusing local development issues, but your remote environment will still benefit from route caching.

About the author

laravelrecipies