Cache Routes with Cloudflare in Laravel

The Cloudflare Cache package for Laravel provides cacheable routes, allowing you to serve millions of requests for static pages efficiently. You can define a group of cacheable routes with the Laravel router, including tags. This package makes it easy to start caching with Cloudflare using the Route::cache() method:

Route::cache(tags: ['tag1', 'tag2'], ttl: 600)->group(function () {
    Route::get('/content_with_tags', function () {
        return 'content';
    });
});

Route::cache(tags: ['staticPages'])->group(function () {
    //
});

This package gives you APIs to purge all content, specific URLs, prefixes/tagged URLs (enterprise), and more. As an example, let's say you want to cache articles (Posts) with Cloudflare and purge the cache whenever the article is updated:

<?php

namespace App\Http\Controllers;

use App\Http\Requests\UpdatePostRequest;
use App\Models\Post;
use Yediyuz\CloudflareCache\Facades\CloudflareCache;

class PostController extends Controller
{
    public function update(Post $post, UpdatePostRequest $request)
    {
        $post->update($request->validated());

        CloudflareCache::purgeByUrls([
            route('post.show', $post->id)
        ]);

        return back()->with('message', 'Post updated and url cache purged');
    }

You can learn more about this package, get full installation instructions, and view the source code on GitHub.


The post Cache Routes with Cloudflare in Laravel appeared first on Laravel News.

Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.