PHP code example of hardimpactdev / cloudflare-cache

1. Go to this page and download the library: Download hardimpactdev/cloudflare-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hardimpactdev / cloudflare-cache example snippets


// bootstrap/app.php
use NckRtl\CloudflareCache\Support\StaticMiddleware;
use NckRtl\Waymaker\Facades\Waymaker;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php', // optional non-Waymaker web routes
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        then: function () {
            // Load Waymaker outside the forced `web` wrapper so `static` is top-level
            Waymaker::routes();
        },
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->group('static', StaticMiddleware::defaults([
            \App\Http\Middleware\HandleInertiaRequests::class,
            // CSP / other cookie-free middleware…
        ]));
    })
    ->create();

// app/Http/Controllers/ProjectsController.php
use NckRtl\Waymaker\Get;

class ProjectsController extends Controller
{
    public static string $middlewareGroup = 'static';

    #[Get(uri: '/', name: 'projects')]
    public function index(): Response { ... }
}

#[Get(uri: '/pricing', name: 'pricing', middlewareGroup: 'static')]
public function pricing(): Response { ... }

#[Get(uri: '/account', name: 'account', middlewareGroup: 'web', middleware: 'auth')]
public function account(): Response { ... }

Route::middleware('static')->group(function () {
    Route::get('/privacy', ...)->name('privacy');
});

// or
Route::get('/pricing', ...)->middleware('cloudflare.cache:86400');

use NckRtl\CloudflareCache\Facades\CloudflareCache;

CloudflareCache::purge('https://example.com/');
CloudflareCache::purge([
    'https://example.com/',
    'https://example.com/projecten/sion',
]);

// Sync (CLI / tests)
CloudflareCache::purge($urls, async: false);

// Purge then warm
CloudflareCache::purge($urls, warm: true);

// Whole zone
CloudflareCache::purgeEverything();

use NckRtl\CloudflareCache\Concerns\PurgesCloudflareCache;
use NckRtl\CloudflareCache\Contracts\PurgesCloudflareUrls;
use Illuminate\Database\Eloquent\Model;

class PortfolioItem extends Model implements PurgesCloudflareUrls
{
    use PurgesCloudflareCache;

    public function cloudflareCacheUrls(): array
    {
        return array_values(array_filter([
            route('projects', absolute: true),
            route('portfolio-item', $this->slug, absolute: true),
            $this->wasChanged('slug')
                ? route('portfolio-item', $this->getOriginal('slug'), absolute: true)
                : null,
        ]));
    }
}

protected function afterSave(): void
{
    CloudflareCache::purge($this->record, warm: true);
}
bash
php artisan vendor:publish --tag=cloudflare-cache-config