31

I'm trying to fully understand the Laravel (5.1) artisan optimize command and best practices, but the documentation seems lacking. I don't have Composer installed on the production server so, specifically, I want to know what files are modified or created when running artisan optimize --force on development that must get pushed to production. The goal being not to blow up the app in production! After running the command, I see the following files have been modified:

\bootstrap\cache\compiled.php
\vendor\composer\ - the entire directory
\vendor\autoload.php

Am I overthinking this, or do I just push these files to production and I'm good to go? Also, what is the best practice regarding when to run artisan optimize? Each time a new model is created? What about controllers, routes and helper classes?

Lastly, I see the \bootstrap\cache\compiled.php file is a whopping 548KB and almost 17K lines! Is that really considered optimal?

1
  • You certainly can simply push compiled.php and the vendor folder to production in order to deploy. And this would in fact have a few advantages over using composer/artisan to do so, such as not relying on additional 3rd party servers to deploy your app. But I've never heard of anyone doing it that way, so I'd hesitate to call it a "best practice".
    – Ben Claar
    Aug 8, 2015 at 23:11

3 Answers 3

31

[edit - As @crishoj says, as of Laravel 5.5, php artisan optimize is no longer needed]

Normal Laravel practice is to have composer installed on your production server.

These are the steps Envoyer (made by Laravel's creator) takes to deploy an app on production -- I've annotated them below:

# Install application dependencies, such as the Laravel framework itself.
#
# If you run composer update in development and commit the `composer.lock`
# file to your repository, then `composer install` will install the exact
# same versions in production.
composer install --no-interaction

# Clear the old boostrap/cache/compiled.php
php artisan clear-compiled

# Recreate boostrap/cache/compiled.php
php artisan optimize

# Migrate any database changes
php artisan migrate
12
  • 10
    composer install --no-interaction --no-dev for optimization on production. Jan 4, 2016 at 22:19
  • 1
    @Ben - I don't use laravel currently - if you do could you try the benchmark? As you said - it should be trivial to run and it would help validate your claims. I'm asking because I tried a similar optimization on a different code base and I didn't see any benefit.
    – Yehosef
    Feb 1, 2016 at 23:31
  • 1
    @Yehosef Well what do you know, you're correct. Running a simple ab -n 500 http://app.vagrant benchmark 4 times, twice with and without compiled.php, all 4 runs were within 1 second of each other. I've removed the claim from the answer. gist.github.com/claar/6ea72454477fc1afb7c8
    – Ben Claar
    Feb 2, 2016 at 18:48
  • 4
    Now we can add also php artisan config:cache and php artisan route:cache, but they must be run on the server because config values are linked to absolute paths and thus won't work if you generate config cache on your dev machine and then copy to the production server. Mar 17, 2016 at 21:53
  • 4
    Running php artisan clear-compiled is not necessary as php artisan optimize overwrites compiled.php. Nov 4, 2016 at 21:27
23

As of Laravel 5.5, php artisan optimize is not longer required.

2
  • 6
    php artisan optimize:clear for clearing the cache is still working well
    – Raskul
    Jan 5, 2022 at 12:03
  • i end up using it ever so often to clear cache. command does what it supposed to do Aug 1 at 20:56
1

You can also take benefit of laravel packages to easily optimize your application by caching page partials

https://github.com/imanghafoori1/laravel-widgetize

1
  • nice package! I've used it in multiple projects, it worked well for optimizing page speed
    – Ali
    Nov 7, 2022 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.