PHP code example of leknoppix / laravel-cloudflare

1. Go to this page and download the library: Download leknoppix/laravel-cloudflare 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/ */

    

leknoppix / laravel-cloudflare example snippets


'providers' => [
    // ...
    Leknoppix\Cloudflare\ServiceProvider::class,
];

'aliases' => [
    // ...
    'CloudflareCache' => Leknoppix\Cloudflare\Facade::class,
];

return [

    /*
    |--------------------------------------------------------------------------
    | Cloudflare Site
    |--------------------------------------------------------------------------
    |
    | Specify the sitename of the Cloudflare.
    |
    */
    'sitename' => env('CLOUDFLARE_SITE', 'test.com'),

    /*
    |--------------------------------------------------------------------------
    | Cloudflare Authentication Email
    |--------------------------------------------------------------------------
    |
    | Specify the authentication email to access Cloudflare.
    |
    */
    'auth_email' => env('CLOUDFLARE_AUTH_EMAIL', '[email protected]'),

    /*
    |--------------------------------------------------------------------------
    | Cloudflare Authentication Key
    |--------------------------------------------------------------------------
    |
    | Specify the authentication key to access Cloudflare.
    |
    */
    'auth_key' => env('CLOUDFLARE_AUTH_KEY', 'test_auth_key'),
];



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Leknoppix\Cloudflare\Model\Concerns\Cloudflare;

class Post extends Model
{
    use Cloudflare;
}




namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Leknoppix\Cloudflare\Events\ModelHasUpdated;

class PurgeCloudflareCache implements ShouldQueue
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  ModelHasUpdated  $event
     * @return void
     */
    public function handle(ModelHasUpdated $event): void
    {
        //handle the Cloudflare purging
    }
}

protected $listen = [
    'Leknoppix\Cloudflare\Events\ModelHasUpdated' => [
        'App\Listeners\PurgeCloudflareCache',
    ]
];

  // purge all files
  \CloudflareCache::purgeAll();

  // purge by urls
  $urls = [
      'http://example.com/posts/post-1',
      'http://example.com/posts/post-2',
  ];
  \CloudflareCache::purgeByUrls($urls);

  // purge by Cache-Tag (only available for Enterprise user)
  $tags = [
      'news-tag',
      'posts-tag',
  ];
  \CloudflareCache::purgeByTags($tags);

  // purge by hosts (only available for Enterprise user)
  $hosts = [
    'https://example.com',
    'https://domain.com',
  ];
  \CloudflareCache::purgeByHosts($hosts);

  // purge by prefixes (only available for Enterprise user)
  $prefixes = [
    '/news',
    '/products/product-1',
  ];
  \CloudflareCache::purgeByPrefixes($prefixes);