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
What about configuration caching ?? to bypass parsing your .env and config files on every app boot you should run
php artisan config:cache
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
To cache a manifest of all of your app's events and listeners
run :
php artisan event:clear
Recreate boostrap/cache/compiled.php
php artisan optimize
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
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
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
yarn add laravel-mix-purgecss --dev
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();
Top comments (3)
You're making some very good points, but I disagree with the wording of this statement:
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:PurgeCSS will not understand that
alert-success
,alert-error
andalert-info
are in use, thus they will be purged in a production build.Instead, you'd use something like this:
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:
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.
@rvxlab you are right brother πβοΈ I couldn't explain it briefly , but u did βΊοΈπβοΈ