DEV Community

Cover image for Make Your Laravel Website Super Fast,Optimize Laravel Performance
Ajay Yadav
Ajay Yadav

Posted on • Updated on

Make Your Laravel Website Super Fast,Optimize Laravel Performance

And Make sure you Like and subscribe πŸ˜‰πŸ‘πŸ‘.

Laravel is the best back-end framework of PHP, and many companies are choosing Laravel for their large and medium size projects. SEO is very important for every website. their are some tips you can follow to make your laravel app super fast.
so lets begin

1. Use Caching in production:

Every time you boot your laravel app , your app determines the middleware , resoles aliases, resolves route groups and identifies the controller action and parameter inputs for every single route entry. so you can think how bad it is for you app in the production.
You can bypass the route processing by caching all routes running this

php artisan route:cache
Enter fullscreen mode Exit fullscreen mode

What about configuration caching ?? to bypass parsing your .env and config files on every app boot you should run

php artisan config:cache
Enter fullscreen mode Exit fullscreen mode

use config() to access .env variables , avoid using env()

You don't need to compile you views every time , just use pre-compiled your blade template views, to do that run this command.

php artisan view:cache
Enter fullscreen mode Exit fullscreen mode

To cache a manifest of all of your app's events and listeners
run :

php artisan event:clear
Enter fullscreen mode Exit fullscreen mode

Recreate boostrap/cache/compiled.php

php artisan optimize
Enter fullscreen mode Exit fullscreen mode

Alert :
You need to clear the cache to reflect any new changes by using the commands

php artisan cache:clear
php artisan view:clear
php artisan route:clear
php artisan clear-compiled
php artisan config:cache
Enter fullscreen mode Exit fullscreen mode

2. Remove Dev dependencies from composer

When you develop your project most probably you will be using some Dev packages to track queries or other development things , remove those packages who are not required in the production.
just run a single command in the production

composer install --prefer-dist --no-dev -o
Enter fullscreen mode Exit fullscreen mode

3. Use Redis, Memcached or dynamoDB Driver

Choosing the right cache,queue and drivers can make a difference to application performance
In production use in-memory cache driver.

For queue jobs use Redis, SQS or Beanstalkd drivers. Database driver is not suitable in production.
For session use Database, Redis, Memcached or DynamoDB drivers.

4. Queue Tasks

Heavy tasks should be queued like sending email, connecting with third party API, uploading large file and updating your search index.

5. Remove unused Services:

In laravel app you will find several services are unused in your product, go to

config/app.php

and comment those services which are unused.

6. Use Laravel ORM over raw query

Larvel comes with Eager loading (ORM) so use it , avoid writing your own raw query.

7. Minifying and Bundling Assets

Laravel mix can help you here, it compiles all your CSS and provide single app.css file, thus reducing multiple HTTP requests to single.
you can also remove unused CSS from your project by using laravel-mix-purgecss package,
just install it in your development project

npm install laravel-mix-purgecss --save-dev
Enter fullscreen mode Exit fullscreen mode
yarn add laravel-mix-purgecss --dev
Enter fullscreen mode Exit fullscreen mode

now in your

webpack.mix.js

const mix = require('laravel-mix');
require('laravel-mix-purgecss');

// ...

mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css')
.purgeCss();
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
rvxlab profile image
RVxLab

You're making some very good points, but I disagree with the wording of this statement:

Larvel comes with Eager loading (ORM) so use it , avoid writing your own raw query.

Avoid writing your own raw query unless you know what you're doing. Sometimes it's needed to write raw queries but when you do so ensure you don't create an SQL Injection vulnerability.

A use case for this is using the Haversine formula to calculate distance between coordinates.

Sometimes doing it yourself is just what you need.

Also on the topic of PurgeCSS you fail to mention that if you're using dynamically built class names PurgeCSS will not work as you'd expect.

As PurgeCSS checks things statically, classes built like so will not be safe unless you put it in the safelist option:

// Assuming status is one of the following: success, error, info

<div class="{{ 'alert-'.$status }}">
    {{ $someAlertMessage }}
</div>
Enter fullscreen mode Exit fullscreen mode

PurgeCSS will not understand that alert-success, alert-error and alert-info are in use, thus they will be purged in a production build.

Instead, you'd use something like this:


function getAlertClass(string $status): string
{
    switch ($status) {
        case 'success':
            return 'alert-success';
        case 'error': 
            return 'alert-error';
        case 'info':
        default:
            return 'alert-info';
    }
}

<div class="{{ getAlertClass($status) }}">
    {{ $someAlertMessage }}
</div>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
juststevemcd profile image
Steve McDougall

I wrote a scope for the haversine formula which enablede to keep using the ORM πŸ˜…

If you're really looking at improving performance of a Laravel application you have two options in 2021:

  • Laravel Vapor
  • Laravel Octaine

Serverless, or a long running process. There are micro optimization a you can do with a Laravel app, but none that make a huge difference straight away.

Collapse
 
ajayyadav profile image
Ajay Yadav

@rvxlab you are right brother 😊✌️ I couldn't explain it briefly , but u did β˜ΊοΈπŸ‘ŒβœŒοΈ