Thursday, 31 January, 2019 UTC


Summary

Since appearing in 2011, Laravel has become a very popular option to develop business-focused applications including information management systems (often referred to as business information management systems). and e-commerce platform. An important aspect of this popularity is the optimization of Laravel’s performance that allows developers to improve the performance of Laravel applications.
Why Businesses Should Focus on Laravel Performance Optimization
The structure of frameworks and related libraries ensure developers can create powerful code with minimum effort. However, code still has room to optimize that can be used to adjust Laravel’s performance to ensure smooth performance after deployment.
Performance and optimization are the two main factors that determine the success of every business application. In this case, securing the performance of Laravel application is an important feature that every developer should provide to their customers. Since Laravel is often used to build business information systems, the performance of the applications provided by Laravel is important for business success. In many cases, information management systems that support decision-making for management classes need to be fast and efficient at all times.
In this article, I have collected some important tips to optimize the performance of Laravel 5.5. I believe these tips will be of great help to Laravel’s developers who are responsible for maintaining business applications supported by Laravel.

Prerequisites

For the purpose of this tutorial, I assume you have installed the Laravel application on the web server. My settings are:
  •      Laravel 5.5
  •      PHP 7.1
  •      MySQL
To make sure I’m not distracted by server-level issues, I decided to use Laravel cloud hosting Cloudways because it concerns server-level issues and is very suitable for storing Laravel project . You can try Cloudways for free by logging in to your account.

Tips to Improve Laravel Performance

  1. Config Caching
Laravel provides a particularly interesting command, Artisan Cache Config is very useful in increasing performance. The basic usage of the order is:

php artisan config:cache
Every time you cache config, the changes you make will not take effect. If you want to refresh the configuration, just run the command again. To delete config cache, use the following command:

php artisan config:clear
  1. Routes Caching
Cache routes are an essential optimization feature, especially for applications with multiple routes and config. Cache routes are a simple array and help speed up Laravel performance because of faster array loading. To do this, we use the following command:

php artisan route:cache
Remember to run the command every time the config or the routes file is changed. Otherwise, Laravel will load the old changes from the cache. To clear the cache, use the following command:

php artisan route:clear
  1. Remove Unused Service
In adjusting Laravel’s performance, an important tip is to not load all services through config. While you are there, always remember to turn off unused services in config files.
  1. Classmap Optimization
Even a middle-level Laravel application has some files because Laravel has a habit of including multiple files for include requests. A simple tip is to declare all files that will be included in the request request and combine them in a single file. Therefore, for all include requests, a file will be called and loaded. To do this, use the following command:

php artisan optimize --force
5. Composer Optimize Autoload
It is a good idea to use the Composer scan application and create one-to-one links of classes and files in the application. Use the following command:

composer dumpautoload -o
  1. Limit Included Libraries
The good point of Laravel is that a large number of libraries can be included in an application. Although this is a good thing, the downside is that the application experience can be slowed down.
This is why it is necessary to review all libraries recalled in the code. If you think you can do it without a library, delete it from config / app.php to speed up the Laravel application. Another important place is composer.json.
  1. JIT Compiler
Translating PHP code to bytecode and then executing it is a resource-intensive process. This is why go-between like Zend Engine is required to execute C subroutines. This process must be repeated every time the application is executed. To reduce time, it is necessary that this process is repeated only once. This is where the Just-in-Time (JIT) compiler proved useful. For Laravel applications, the proposed JIT compiler is HHVM for Facebook.
  1. Choose a Fast Cache and Session driver
To adjust Laravel’s optimal performance, it is best to store cache and session in RAM. I believe the fastest cache and session driver for Laravel 5’s performance is Memcached.
The driver key to change the session driver is usually in app / config / session.php. Similarly, the driver key to change the driver cache is in app / config / cache.php.
  1. Cache Queries Results
Caching the results of frequently run queries is a great way to improve Laravel 5.5’s performance. For this, I recommend using remember function, which is used as follows:

$posts = Cache::remember('index.posts', 30, function()
 
{return Post::with('comments', 'tags', 'author', 'seo')->whereHidden(0)->get();});
  1. Use “Eager Loading” for Data
Laravel provides a great ORM to handle databases. Known as Eloquent, it creates abstracts model from tables in the database. Using simple constructs, developers can use Eloquent to handle all CRUDs in PHP. When the Eloquent uses the eager loading, it will retrieve all the model objects linked to the initial query. This is added to the application’s response. Compare lazy loading and eager loading:

Lazy loading query will look like this:


$books = App\Book::all();
 
foreach ($books as $book) {
 
echo $book->author->name;}
Correspondingly, the eager loading query will look like this:

$books = App\Book::with('author')->get();
 
foreach ($books as $book) {
 
echo $book->author->name;
 
}
  1. Precompile Assets
To adjust the Laravel application, developers often distribute code into separate files. Although this keeps the code clean (code clean) and manageable, it does not contribute to an effective product. To help developers in this context, Laravel provides a simple command:

php artisan optimize
 
php artisan config:cache
 
php artisan route:cache
Use CDN for Delivering Static Assets
Downloading static assets files from the CDN server (as opposed to downloading directly from the file host) will improve Laravel’s application performance.
When the client accesses a site, part of the information is provided from the nearest CloudwaysCDN area. This result is basically a fast and amazing page stack for the client. CloudwaysCDN is a CDN-based benefit. This means that you must describe static resources (JS, CSS, HTML, images, recording, etc.) on a specific application.
Source: https://www.cloudways.com/blog/laravel-performance-optimization/

Share This:

The post Laravel: 12 tips to optimize performance appeared first on FrontNet Blog.