Laravel Magazine
Speed Up Laravel with Config, Route, and View Caching

Speed Up Laravel with Config, Route, and View Caching

Eric Van Johnson ·

Before you reach for exotic performance tuning, make sure you are using the optimization commands Laravel already ships. In production they meaningfully cut per-request overhead, and they cost nothing but a line in your deploy script. Here is what each one does, and the one rule that keeps them from biting you.

Config Caching

Normally Laravel reads and parses every file in config/ on each request. config:cache compiles them all into a single cached file.

php artisan config:cache

There is one critical catch: once config is cached, calls to the env() helper outside of config files return null. The cached config no longer reads .env. The rule is simple and absolute: only ever call env() inside config/ files, and everywhere else in your app read values through config().

// Bad, breaks under config:cache
$key = env('BILLING_API_KEY');

// Good
$key = config('services.billing.key');

Route Caching

Registering routes on every request adds up, especially in large apps. route:cache serializes them into a fast lookup file.

php artisan route:cache

The catch here is that route caching only works with controller-based routes. If you define a route with a closure, the command will fail, because closures cannot be serialized. Point those routes at invokable controllers instead and you are fine.

View Caching

Blade templates compile to plain PHP the first time they render. view:cache compiles all of them ahead of time so the first visitor after a deploy does not pay the compilation cost.

php artisan view:cache

Event and the Catch-All Command

You can also cache event-to-listener mappings with event:cache. Rather than remember each command, optimize runs the useful ones together:

php artisan optimize

This caches config, routes, events, and more in one step. Its inverse, optimize:clear, flushes all of them.

The One Rule for Your Deploy Script

Caching freezes the current state, so a stale cache serves old config or routes. The fix is to always re-run these commands as the last step of every deploy, after your new code is in place.

php artisan optimize:clear   # drop stale caches
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan optimize          # rebuild fresh caches

Equally important: do not cache config in local development. A cached config means editing .env has no effect, which produces baffling "why isn't my change working" sessions. Keep caching for production and staging, and if you ever cached locally by accident, php artisan optimize:clear sets things right.

The Payoff

None of these commands change your code, yet together they remove config parsing, route registration, and view compilation from the hot path of every request. It is close to free performance, as long as your deploy rebuilds the caches and you keep env() confined to config files.

Further Reading

Stay Updated

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.