Code // Sep 27, 2016

Laravel 5 Artisan Optimization Commands

 

A while ago, I was facing my worst nightmare for two straight days. It was no joke (I was even working on it in my sleep).

Below were the issues I was facing, that made me dig deeper into the topic:

  1. Whenever I edited the config/app.php file, it did nothing. It was as if the file didn’t exist. Even when I temporarily deleted the file, everything still worked fine!
  2. Route just was not responding to changes, even after adding new lines of code to the file.

 

Laravel 5 Artisan optimization – A brief introduction…

If you’re a bit familiar with Laravel, you’d know that Laravel optimization doesn’t really need much introduction as it is self-explanatory:

Laravel comes with a set of artisan commands that optimize the framework for better performance.

This article explains what the commands actually do and where the cache files are stored.

These are the commands that we will be focusing on:

  • php artisan optimize
  • php artisan config:cache
  • php artisan route:cache

The optimization files generated by artisan optimization are stored in the bootstrap/cache/ directory.

 

1. php artisan optimize

php artisan optimize creates a compiled file of commonly used classes in other to reduce the amount of files that must be included on each request.

After running the command, all commonly used classes are compiled and then saved to this file directory: bootstrap/cache/compiled.php.

You can specify additional classes to be included by adding them to config/compile.php.

The compiled file is only created in production stage, not unless this attribute is added –force (that is, php artisan optimize –force).

 

2. php artisan config:cache

php artisan config:cache combines all configuration (config) files into one file for quick loading.

This cache file is stored in bootstrap/cache/config.php

Use php artisan config:clear to undo the command.

 

3. php artisan route:cache

php artisan route:cache creates a cache file located in the Bootstrap/cache/routes directory for faster loading. It basically registers all routes for easy access.

After running the above command, the app will use the cached file instead of the original route.php file.

Use php artisan route:clear to delete the cache files.

 

In addition to the commands mentioned above, here is one I’d like to add:

4. php artisan view:clear

This command is used to clear all view (resource/view/) cached files which are generated whenever php artisan optimize command is executed.

The cached files are stored in the vendor/ directory by default, if the directory is writable. If not, it’s stored in the storage/framework/ directory.

 

Thanks for reading thus far! I do hope you’ve found this useful.

As for those nightmares of mine, I was able to fix them after understanding how these commands work.

 

3 thoughts on “Laravel 5 Artisan Optimization Commands”

  1. This is so much basic commands for an artisan, it’s possible use too.

    php artisan cache:clear,
    php artisan view:clear,
    php artisan config:clear,

    Composer have commands to optimize.

    1. Thanks a lot for your addition, José.

      Yeah, there are so many other artisan commands, but this article was meant to focus only on Artisan Optimization commands (relating to the issue I had).

      In the near future I’ll be doing something on “How to create a custom artisan command”.

      Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *