imageBlog

Written By

How to speed your laravel app

How to speed your laravel app 

Website speed (aka website performance) refers to the amount of time it takes a browser to load fully operational web pages from a specific website. Therefore, it has a direct impact on user experience (UX) and conversion rates. There are many tips to speed up your website. We will explain some of them.

 

Config Caching

 

Laravel provides the Artisan Cache Config command that helps in performance boosts. Here’s how you can use the command:

 

php artisan config:cache

 

Once you cache the config, any changes you make won’t have any effect. If you wish to refresh the config, rerun the above command. Use the following command to clear the config cache:

 

php artisan config:clear

Once you cache the config, any changes you make won’t have any effect. If you wish to refresh the config, rerun the above command. Use the following command to clear the config cache:

 

php artisan config:clear

 

You should refrain from executing the config cache command during local development as the configuration settings may require frequent changes throughout your application development.

You can use OPcache to optimize the application further, which caches the PHP code, so you don’t need to recompile it.

 

Routes caching is an essential optimization feature, particularly for apps with many routes and configurations. It is a simple array and helps speed up Laravel performance due to faster array loading. Execute the following command to route caching:

 

php artisan route:cache

 

Remember to run the command every time the config or route files have been changed. Otherwise, Laravel will load old changes from the cache. Clear the cache by using the following command:

 

php artisan route:clear

Remove Unused Service

Laravel’s primary goal is to ease the development process for developers. Once you launch Laravel, it auto-loads many service providers listed within the config/app.php file to help you get started with your project.

 

You don’t usually need services like View Service or Session Service. In addition, many developers don’t follow the default framework settings. So you should disable the unnecessary services to optimize Laravel’s performance.

 

Use “Eager Loading” for Data

Laravel offers Eloquent, a great ORM to deal with databases. It creates models that abstract the database tables from the developers.

Using simple structures, developers can use Eloquent to deal with all CRUD operations in PHP. When Eloquent uses eager loading, it retrieves all associated object models in response to the initial query, adding to the application’s response.

 

Let’s compare lazy loading and eager loading. The lazy loading query will look like this:

 

 

In contrast, eager loading query will look like:



 

Use Queues

Offloading sluggish tasks to a queue job is a simple technique to rapidly maximize the speed of your Laravel application.

 

Sometimes you don’t need the information in the UI right away. In this case, such tasks can be postponed and run later in the background by a separate process (e.g. sending an email). This can significantly increase the performance of your app’s online requests.

 

Laravel supports a variety of queue drivers such as IronMQ, Redis, Amazon SQS, and

Beanstalkd. It additionally includes a built-in queue worker that can be executed using the following command:

 

php artisan queue:work

You can add a new job into the queue using this method:

 

Queue::push('SendEmail', array('message' => $message));

Use the method below via Carbon if you want to defer the execution of one of the queued jobs. For example, say you want to schedule a job that sends an email to a client 10 minutes after they create an account:

 

$date = Carbon::now()->addMinutes(10);

 

Queue::later($date, 'SendEmail@send', array('message' => $message));

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.

 

Use Lumen for Small Projects

There are occasions when developing a small application (e.g. mobile or Angular apps) doesn’t demand the use of a full-stack framework like Laravel. In this scenario, consider using Lumen instead.

 

Lumen is a microframework developed by the same creator of Laravel. Like a lighter version of Laravel, Lumen is all about speed and performance for microservices. It requires minimal settings and alternative routing parameters when building web apps, allowing for a faster development process.



Are you ready?lets work