At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

11 Quick Tips To Optimize A Laravel Application

  • By Vignesh Thandapani
  • September 27, 2017
  • 1093 Views

There are several ways you can improve the performance of the Laravel application. Recently I have developed an application using the Laravel framework. Even though the possibility of having performance issues while using this framework is quite less. But when developing complex applications developers may forget to focus on the performance-related things while focusing on application requirements and its logic. So developers have to emphasize on the performance of the application also. Following small tips will greatly improve the performance of the application. Out of my learnings and after referring many resources I have written these tips which I also used practically to improve the performance of my Laravel application.

1. Config caching

We know that the config is present across many files, and we have to include each of the files for every request. So combining all the files will be a good solution for this and you can do it using the following command.

php artisan config:cache

Once you cache the config, the changes you make do not have any effect. We can refresh the config by running the above command once again. In order to clear the config cache use the below command.

php artisan config:clear

2. Routes caching

The routes.php file can be cached by running the following command:

php artisan route:cache

When we use closures the artisan command throws an exception when we try to compile the routes, So it is recommended to move them into the controllers.

Just like the config cache, The changes you make in routes.php file will not have any effect once you cache it.  You can refresh the routes cache by running the above command again. If you want to clear the routes cache use the below command:

php artisan route:clear

3. Classmap optimization

A complex project will have numerous files, for every request Laravel includes many different files. For this, it is recommended to declare all the files which are used for each request and combine them into one file. So that this file will be loaded for every request. This can be done using the following command:

php artisan optimize --force

4. Optimize composer autoload

The following command makes the composer scan the complete application and allows it to create one to one association of all the classes and files in the application

composer dumpautoload -o

This works for any application that uses the composer.

Once you have done the class-map optimization by running PHP artisan optimize, The command will also tell the composer to create an optimized autoload. So it is not needed to run the above command again.

ALSO READ: Laravel vs Symfony – Which PHP Framework You Should Choose?

5. JIT Compiler

We all know that PHP needs something like the Zend engine to interpret the files and execute C routines. But this process takes time. For this, it is recommended to use JIT Compiler. This helps the application to act fast. Widely used JIT compiler for Laravel is HHVM.

6. Choose a faster cache and session driver

To access the data faster, the best way is to store the cache and session entries in RAM, Out of all the cache and session drivers that Laravel supports I recommend Memcached.

For changing the session driver, The driver key can be found in the following file:

app/config/session.php

For changing the cache driver, The driver key can be found in the following file:

app/config/cache.php

7. Cache the queries results

Caching the results of the queries that are frequently run would be another great way to improve the performance, For this, we use the remember function as follows:

$posts = Cache::remember('index.posts', 30, function()
{
return Post::with('comments', 'tags', 'author', 'seo')->whereHidden(0)->get();
});

8. Minimize Plugins

Laravel offers many plugins, and it is true that these plugins add more functionalities. And obviously, the number of libraries and files that has to be load also increases with this. This can slow down things. So it is recommended to cut down the unnecessary plugins. We know that Laravel will use the composer to manage components, so removing the useless parts from composer.json file reduces the dependencies to be loaded.

9. Use “Eager Loading” of  Data

In order to map the object models to database tables, Laravel use Eloquent ORM. But this uses a lazy loading approach which would affect the performance of the application. But with the Eager Loading, the queries will retrieve any associated object models as part of your initial query. Eager loading will reduce the N + 1 problems.

ALSO READ: 5 Smart Ways to Develop your Business with Laravel Web Development

The lazy loading query will look like the following:

$books = App\Book::all();
foreach ($books as $book) {
echo $book->author->name;
}

The eager loading query will look like the following:

$books = App\Book::with('author')->get();
foreach ($books as $book) {
echo $book->author->name;
}

10. Precompile Assets

For keeping our code clean and maintainable we generally maintain separate files like configuration files and route files etc. But this only makes the code maintainable but does not profit in any way for production. So Laravel provides us with the following commands that combine these files into one.

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

Also for the CSS and JS fields, we had Elixir.I have already explained how to install and use Elixir here.

ALSO READ: Laravel Development : How to force a browser to refresh CSS and JS files on an update

11. Use CDN for delivering your static assets.

Loading the static assets files from the CDN server will improve the performance of loading.

Conclusion :

So when developing complex applications, following the above points will definitely improve the performance of the application. To know more about Laravel web development follow Agiratechnologies.

Looking for a Tech partner to dominate the digital world?

Vignesh Thandapani

An enthusiastic Tech Lead with 6 plus years of experience in Web development arena. Owns legitimate experience in CorePHP, Laravel, Symfony, CakePHP, Wordpress, Joomla. Behalf, a young Aspiring "Travel admirer" craves to live with Nature.