Make Your Laravel Website Super Fast,Optimize Laravel Performance
Tags: laravel, php, vue, javascript
Published: 2021-04-11 16:18:45


{% youtube jnGXHqWx7dQ%}

*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
php artisan route:cache
```
What about configuration caching ?? to bypass parsing your .env and config files on every app boot you should run
```php
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
php artisan view:cache
```
To cache a manifest of all of your app's events and listeners 
run :
```php
php artisan event:clear
```

```Recreate boostrap/cache/compiled.php```
```php
php artisan optimize
```

__Alert :__
You need to clear the cache to reflect any new changes by using the commands 
```php
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

```php
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
```js
npm install laravel-mix-purgecss --save-dev
```
```js
yarn add laravel-mix-purgecss --dev
```

now in your ``` webpack.mix.js ```

```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();
```