Laravel has been a famous framework since 2011. This framework can help us develop a project faster than it did with a lower effort. But one of the most important things when we use a framework is performance.
Luckily, Laravel has supported many helpful features to optimize performance (Redis, Memcache, DB support, etc).
There are many factors that can negatively affect your web application, including poor code quality, inefficient server configurations, and insufficient resources.
However, we can use some tips to improve your website.
1. Update to the latest version of PHP and Laravel
It seems quite obvious but it is the simple way to improve your web application. This may be a challenge with a large legacy application while updating the version. But using the latest version can help us with numerous benefits: new features, bug fixes, and security.
2. Using the Artisan Command
One of the best features in Laravel is called Artisan. Artisan is a command-line tool. It will improve your web performance if you use it in the right way. The easy way is that you can cache the route as well as config with a command line.
|
php artisan config: cache php artisan route: cache |
Route Caching: This is a critical tool, especially for applications that have a large number of routes. With caching, Laravel only refers to the file where the cached routes are stored (bootstrap/cache/routes.php) instead of rendering all the routes in your website (This will cause slow performance).
Config Caching: Similar to route caching, it will find all data of the config file and variables of .env file then store it in bootstrap/cache/config.php
Remember that you need to run the command above if you have changed any in the config file or route file. To clear the cache, you can run these commands:
|
php artisan config: clear php artisan route: clear |
3. Remove unused service
The key goal of Laravel is to bring an easy development process to the developers.
When launching Laravel, it will auto-load many service providers listed within the config/app.php file to help you get started with your project.
In addition, many developers don’t want to follow the default framework settings. So it’s necessary to delete unused services to improve your project performance.
4. Eager Loading
All of the queries in Laravel are lazy queries when you execute. It only fetches the required data. Sometimes, this will increase the number of queries and cause low performance for your website. You will run up to N+1 queries to find your response.
To resolve the N+1 queries problem, Laravel provides us with a feature that can use eager loading to load the data to help us improve performance.
Let's compare the source code of two loading modes:
Lazy Loading: You will need to execute multiple queries to find the response you need.
|
$books = Book::all(); // Get all books foreach($books as $book) { echo $book->author->name // Executing query to find author link with book } |
Eager Loading: You only need to execute once.
|
$books = Book::with('author')->get(); // Get all books link with author foreach ($books as $book) { echo $book->author->name // Not execute query again to find } |
Conclusion
Laravel is a fast-growing PHP framework, that has many useful features to support users to improve the performance of their websites.
Hopefully, some tips that I introduced above can help you somewhat in optimizing your website performance.
Reference: