Make Your Laravel Website Super Fast,Optimize Laravel Performance

ajayyadav

Ajay Yadav

Posted on April 11, 2021

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
💖 💪 🙅 🚩
ajayyadav
Ajay Yadav

Posted on April 11, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related