DEV Community

AutoIdle
AutoIdle

Posted on • Originally published at Medium on

Laravel Deployment Optimization on Heroku

Laravel on Heroku — Tip #4

Photo by Todd Quackenbush on Unsplash

Laravel has great documentation about deployment.

The most important part is optimization, which includes 3 parts:

  1. Autoloader Optimization
  2. Optimizing Configuration Loading
  3. Optimizing Route Loading

On Heroku, we also need these 3 optimizations.

Autoloader Optimization

We have to optimize Composer’s class autoloader with the following command:

composer install --optimize-autoloader --no-dev
Enter fullscreen mode Exit fullscreen mode

Heroku already run composer install with this command (Doc). Thanks Heroku 👍

️Optimizing Configuration Loading and Route Loading

We should run

php artisan config:cache

php artisan route:cache
Enter fullscreen mode Exit fullscreen mode

for optimizing configuration loading and route.

We will call these 2 artisan commands on boot so we create a new scripts entry in composer.json, e.g. warmup:

"scripts": {
 "warmup": [
 "php artisan config:cache",
 "php artisan route:cache"
 ]
}
Enter fullscreen mode Exit fullscreen mode

You may then combine invocation of that script together with your existing command in Procfile, for example:

web: composer warmup && $(composer config bin-dir)/heroku-php-apache2 web/
Enter fullscreen mode Exit fullscreen mode

That's it, our Laravel app on Heroku is optimized! 🚀

Want more tips like these?

You should follow me on Twitter! And if you’re building on Heroku, you should check out AutoIdle — the automated way to save money on your staging and review apps.

Discussion (1)

Collapse
wrbkor23 profile image
Phạm Tiến Hải

will heroku remove the cache file after a specific time, because it is temporary file