Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
laravel
community creator

How to make your Laravel application faster

Chinweuba Elijah Azubuike

Overview

Optimizing the speed of your website is very important. In this shot, you will learn how to improve the overall speed of your Laravel web application in a few steps.

Step 1

Install the page cache package using composer.


composer require silber/page-cache

Step 2

Register the cache middleware to http/kernel.php like so:


'page-cache' => \Silber\PageCache\Middleware\CacheResponse::class,

Now add the middleware to routes that will have high traffic, like so:


Route::get('/post/{slug}', 'SiteController@post')->middleware('page-cache');

Step 3

Edit your .htaccess file with the code snippet below:


RewriteCond %{REQUEST_URI} ^/?$
RewriteCond %{DOCUMENT_ROOT}/page-cache/pc__index__pc.html -f
RewriteRule .? page-cache/pc__index__pc.html [L]
RewriteCond %{DOCUMENT_ROOT}/page-cache%{REQUEST_URI}.html -f
RewriteRule . page-cache%{REQUEST_URI}.html [L]
RewriteCond %{DOCUMENT_ROOT}/page-cache%{REQUEST_URI}.json -f
RewriteRule . page-cache%{REQUEST_URI}.json [L]

Step 4

Clear and update the page-cache using this artisan command:


php artisan page-cache:clear {slug}

Use the model boot method to maintain dynamic deletion and update of the cached page.

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Artisan;

class Post extends Model {
	
	public static function boot() {
	    parent::boot();

    static::updated(function ($model) {
	        Artisan::call("page-cache:clear $model->slug");
	    });
	}
}

RELATED TAGS

php
laravel
community creator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring