10 simple tips for optimizing your Laravel application

Subscribe to our monthly newsletter to get the latest news and handpicked content delivered to your inbox.

Back to blog

February 7th, 2022, posted in Guest Posts
by Bobby Iliev

Here at UPDIVISION, we love to innovate. But sometimes it’s just as awesome to go back to the basics, to review some key things about our favorite technologies. So we’ve started a series of guest posts, where we invited developers from around the world to share their knowledge. To read more articles from this series, go here.

 

Laravel was created by Taylor Otwell and was initially released in June 2011. It is an open-source PHP framework that is used to build web applications. It is without a doubt the most popular PHP framework and even one of the most popular back-end frameworks in general.

 

In this article, we’ll take a look at 10 simple tips for optimizing your Laravel application so that it runs faster and more efficiently.

 

1. Cache your routes

Caching routes is a great way to speed up your application. It’s a great way to avoid having to parse the routes every time you need to access a route.

 

This is especially important when deploying your application to a production environment.

 

To cache your routes, you can use the following command:

php artisan route:cache

 

To clear the route cache, you can use the following command:

php artisan route:clear

 

2. Cache your config

You can use the following artisan command to cache your config:

php artisan config:cache

 

In order to make sure that this works as expected, an important note here is that you should only use the env() helper function in your config files and use the config() helper function throughout your application.

 

3. Cache your views

To cache your views, you can use the following command:

php artisan view:cache

 

This will cache all of your views in the storage/framework/views directory. You might want to run this command during your deployment process.

 

4. Composer Autoload Optimization

For larger applications, you might want to use the composer autoload optimization feature as the autoloader can take significant time for each request:

composer dumpautoload -o

 

The -o flag will convert PSR-0/4 autoloading to classmap to get a faster autoloader.

 

5. Remove unused services

As your application grows, you might want to remove unused services. This can be done by removing the service from the config/app.php file.

 

Keep in mind that you should only remove services that you don’t use.

 

6. Remove unused packages

It is best to keep your app clean and remove unused packages from your application.

 

This can be done by removing the package from the composer.json file or by running the following command:

composer remove package-name-here

 

7. Remove development dependencies

While on the composer packages note, it is important to mention that you should remove development dependencies when deploying to production. This can be done by running the following command:

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

 

8. Precompile your assets

In case you are using npm for your front-end development, you might want to precompile your assets. This can be done by running the following command:

npm run production

 

This will compile your assets and create a public/assets directory.

 

9. Use a CDN

There are several CDN providers that you can use to speed up your application. For example, a good CDN provider is Cloudflare and they offer a free plan that allows you to use their CDN service.

 

This should reduce the load on your server and the latency of your application.

 

Speaking of servers, you should also make sure that you have a good server that is optimized for your application.

 

10. Optimize your database

If you’re using MySQL, you can use a tool like MySQLTuner to analyze your database to get a list of the values that could be tweaked to improve performance.

 

Releem is also a great tool to analyze your database for performance improvements.

 

To take this a step further, you can use Materialize to speed up your read queries. Materialize is a streaming database that takes data coming from different sources like Kafka, PostgreSQL, S3 buckets, and more and allows users to write views that aggregate/materialize that data and let you query those views using pure SQL.

 

Conclusion

For more information about Laravel, check out the official documentation.

 

As further reading, you can also check out this post by Taylor Otwell where he benchmarks Laravel and other popular PHP frameworks:

 

For more useful tips make sure to follow me on Twitter: @bobbyiliev_!


About the author

Bobby Iliev

I am a professional Linux DevOps Engineer and a Laravel developer with a demonstrated history of working in the internet industry. I am an avid Linux lover and supporter of the open-source movement philosophy.

See more articles by Bobby Iliev