PHPackages                             yediyuz/laravel-cloudflare-cache - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. yediyuz/laravel-cloudflare-cache

ActiveLibrary[Caching](/categories/caching)

yediyuz/laravel-cloudflare-cache
================================

laravel-cloudflare-cache

v2.1.0(7mo ago)28239.2k↓30.7%9[4 PRs](https://github.com/yediyuz/laravel-cloudflare-cache/pulls)MITPHPPHP ^8.2CI passing

Since Dec 21Pushed 5mo ago4 watchersCompare

[ Source](https://github.com/yediyuz/laravel-cloudflare-cache)[ Packagist](https://packagist.org/packages/yediyuz/laravel-cloudflare-cache)[ Docs](https://github.com/yediyuz/laravel-cloudflare-cache)[ RSS](/packages/yediyuz-laravel-cloudflare-cache/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (13)Versions (14)Used By (0)

   ![Package Image](https://camo.githubusercontent.com/09170ee41709e06765936c9de9b690ed58cf129d907b3c0b2cc028f90907967a/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c253230436c6f7564666c61726525323043616368652e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d7965646979757a2532466c61726176656c2d636c6f7564666c6172652d6361636865267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d53657276652b6d696c6c696f6e732b6f662b72657175657374732b62792b63616368696e672b776974682b436c6f7564666c617265266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d736572766572)Laravel Cloudflare Cache
========================

[](#laravel-cloudflare-cache)

 [![Test Status](https://camo.githubusercontent.com/8af63562d9f2a5f78ed7ceb34d896c2922fc9153254ca6d14ca005abcf114ead/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7965646979757a2f6c61726176656c2d636c6f7564666c6172652d63616368652f74657374732e796d6c3f6272616e63683d6d6173746572266c6162656c3d7465737473)](https://github.com/yediyuz/laravel-cloudflare-cache/actions) [![Latest Release](https://camo.githubusercontent.com/4bc5721d167d7d0225ba6132f3419ca4fb059397ae98ec09fb04934386bc194a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7965646979757a2f6c61726176656c2d636c6f7564666c6172652d63616368652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/yediyuz/laravel-cloudflare-cache) [![License](https://camo.githubusercontent.com/88557f930d685591f597dec0a07d2ba4493556477e83338bda39ca0fd9591fde/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e7376673f6c6162656c3d6c6963656e7365)](https://github.com/mertasan/tailwindcss-variables/blob/master/LICENSE)

You can serve millions of requests with this package. This package provides cacheable routes for Cloudflare. Thanks to Cloudflare, your static pages are served efficiently, reducing the load on your servers if they are cached for the TTL (Time to Live) duration. You can purge the cache whenever you need with this package.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require yediyuz/laravel-cloudflare-cache
```

You can publish the config file with:

```
php artisan vendor:publish --tag="cloudflare-cache-config"
```

Add environment variables to .env file

##### Using global api key:

[](#using-global-api-key)

```
CLOUDFLARE_CACHE_EMAIL=info@example.com #Cloudflare account email address
CLOUDFLARE_CACHE_KEY=XXXXXXX #Cloudflare global api key
CLOUDFLARE_CACHE_IDENTIFIER=XXXXXXX #ZONE_ID
CLOUDFLARE_DEFAULT_CACHE_TTL=600 #10 minutes
CLOUDFLARE_CACHE_DEBUG=false
```

##### Using fine-grained api token:

[](#using-fine-grained-api-token)

```
CLOUDFLARE_CACHE_API_TOKEN=XXXXXXX
CLOUDFLARE_CACHE_IDENTIFIER=XXXXXXX #ZONE_ID
CLOUDFLARE_DEFAULT_CACHE_TTL=600 #10 minutes
CLOUDFLARE_CACHE_DEBUG=false
```

### Add `Rule` on Cloudflare

[](#add-rule-on-cloudflare)

To active caching on static pages, you need to add `page rule` **OR** `cache rule` on Cloudflare.

For page rule:

- If the URL matches: `www.example.com/*`
- Setting: Cache Level
- Value: Cache Everything

For the cache rule:

- Field: hostname
- Operator: equals
- Value: `example.com`
- Then: Eligible for cache

Usage
-----

[](#usage)

### Define routes to cache

[](#define-routes-to-cache)

You can use cache groups for your static contents.

```
Route::cache()->group(function () {
    Route::get('/content', function () {
        return 'content';
    });
});
```

You can use cache tags, so you can clear your caches easily. Specify custom ttl for expire time in seconds. When you do not pass ttl, it will use default ttl given in the config.

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

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

Warning

Be careful caching your routes! Do not cache your dynamic pages such as admin panel or form based pages!

### Clear Cache

[](#clear-cache)

#### Purges everything

[](#purges-everything)

```
CloudflareCache::purgeEverything()
```

#### Purges by urls

[](#purges-by-urls)

```
CloudflareCache::purgeByUrls([
    'https://example.com/hello',
])
```

#### Purges by prefixes

[](#purges-by-prefixes)

[https://developers.cloudflare.com/cache/how-to/purge-cache/purge\_by\_prefix/](https://developers.cloudflare.com/cache/how-to/purge-cache/purge_by_prefix/)

```
CloudflareCache::purgeByPrefixes([
    'www.example.com/foo',
])
```

#### Purges by tags

[](#purges-by-tags)

```
CloudflareCache::purgeByTags([
    'staticPages',
])
```

#### Purges by hostname

[](#purges-by-hostname)

```
CloudflareCache::purgeByHosts([
    'www.example.com',
    'images.example.com',
])
```

Post update example to clear cache

```
