How to Optimize Laravel Performance

1. Route Caching

Route caching is an excellent feature, especially for apps with a large number of configurations and routes spread across the code. It’s a group of routes packed in a single command to help reduce the tedious task of charting your routes manually. As a result, your website’s pages will load a lot faster.

Route caching allows Laravel to retrieve routes periodically from the pre-compiled cache rather than having to start from the ground up for each new user.

Use this command to cache the routing data required:

 

php artisan route:cache

 

Keep in mind that the cache expires when the user leaves your site. It’s also important to run that route cache command every time after making structural changes (e.g. routes files and config) to your website, as any modifications made afterward won’t take effect.

If you want to clear the route cache, run the following command:

php artisan route:clear

2. Optimize Composer

Laravel uses a separate tool called Composer to manage different dependencies. When you initially install Composer, it loads dev dependencies into your system by default.

These dependencies are useful for developing a website. But once your site is fully operational, they’re no longer required, and in fact, they’ll only slow it down.

When utilizing Composer to install packages, use the --no-dev and -o parameters as follows to remove dev dependencies:

composer install --prefer-dist --no-dev -o

This command allows Composer to create a directory for optimizing the autoloader and boosting performance. It simply requests the official distribution to be retrieved and packaged, with no dev dependencies.

Be careful not to eliminate any runtime dependencies. This could jeopardize your website’s performance or even cause it to crash.

3. Reduce Autoloaded Services

The goal of Laravel is to make the development process as breezy for devs as possible. When you launch Laravel, for example, it auto-loads a large volume of service providers listed in the config/app.php file to help you get started with your project quickly.

While this is a beneficial step by Laravel, you won’t need to use all of these services for building an application.

Take the REST API for instance. You don’t require services such as View Service Provider or Session Service Provider. In addition, many developers don’t follow the default framework settings. You can simply disable services that are superfluous to your needs (e.g. Pagination Service Provider, Translation Service Provider, Auth Service Provider, etc).

You’ll be able to improve the speed of your Laravel applications by applying the same principle to other apps. Just make sure you don’t remove any important services, and double-check everything before you drop the hammer.

4. Use Artisan Commands and Cache Effectively

Artisan is a popular command-line tool that comes with Laravel. It makes it easy for developers to carry out recurring and complex tasks automatically. Website creators can also use it to conduct tests and generate commands.

Using Artisan commands cleverly can amp up your app performance. Below, we’ve listed several of the best caching commands you can utilize.

Configuration Caching

Cache config is an excellent command to get a speed boost. It compiles all of your application’s configuration values into one file so that the framework can load faster. All you need is to run:

php artisan config:cache

Note that you shouldn’t execute the config cache command during local development. This is because configuration settings may need to be changed often throughout the development of your app.

To clear the config cache, run this command:

php artisan config:clear

Views Caching

The view cache is another aspect of the application that contains a cache. The view cache stores generated Blade templates to increase the speed of your project. You can use the artisan command below to compile all views manually and optimize performance:

php artisan view:cache

Remember to clear the cache when you upload a new code; otherwise, Laravel will use your old views and you will spend lots of time trying to troubleshoot this. Run this command to clear the view cache:

php artisan view:clear

Application Caching

This is the main cache in Laravel. It saves all the data that you cache manually in your app. Using Laravel’s cache is a smart approach to speed up commonly accessed data and optimize Laravel performance. If you use tags or multiple cache storage, you can flush only certain elements of the cache.

Here’s the artisan command to clear the Laravel cache:

php artisan cache:clear

Keep in mind that this command won’t delete any route, config, or view cache located in the /bootstrap/cache/ folder.