@@ -26,11 +26,13 @@ Varnish ✓ ✓ ✓ ✓
2626Fastly ✓ ✓ ✓ ✓
2727NGINX ✓ ✓
2828Symfony Cache ✓ ✓ ✓ (1) ✓ (1)
29+ Cloudflare ✓ ✓ (2) ✓
2930Noop ✓ ✓ ✓ ✓ ✓
3031Multiplexer ✓ ✓ ✓ ✓ ✓
3132============= ======= ======= ======= ======= =======
3233
33- (1): Only when using `Toflar Psr6Store `_.
34+ | (1): Only when using `Toflar Psr6Store`_.
35+ | (2): Only available with `Cloudflare Enterprise`_.
3436
3537If needed, you can also implement your own client for other needs. Have a look
3638at the interfaces in namespace ``FOS\HttpCache\ProxyClient\Invalidation ``.
@@ -90,7 +92,8 @@ Varnish Client
9092~~~~~~~~~~~~~~
9193
9294The Varnish client sends HTTP requests with the ``HttpDispatcher ``. Create the
93- dispatcher as explained above and pass it to the Varnish client::
95+ dispatcher as explained :ref: `above <HTTP client configuration >` and pass it to
96+ the Varnish client::
9497
9598 use FOS\HttpCache\ProxyClient\Varnish;
9699
@@ -163,11 +166,12 @@ Fastly Client
163166~~~~~~~~~~~~~~
164167
165168The Fastly client sends HTTP requests with the ``HttpDispatcher ``. Create the
166- dispatcher as explained above and pass it to the Fastly client::
169+ dispatcher as explained :ref: `above <HTTP client configuration >` and pass it to
170+ the Fastly client::
167171
168172 use FOS\HttpCache\ProxyClient\Fastly;
169173
170- $varnish = new Fastly($httpDispatcher);
174+ $fastly = new Fastly($httpDispatcher);
171175
172176.. note ::
173177
@@ -195,13 +199,14 @@ A full example could look like this::
195199 ];
196200 $requestFactory = new MyRequestFactory();
197201
198- $varnish = new Fastly($httpDispatcher, $options, $requestFactory);
202+ $fastly = new Fastly($httpDispatcher, $options, $requestFactory);
199203
200204NGINX Client
201205~~~~~~~~~~~~
202206
203207The NGINX client sends HTTP requests with the ``HttpDispatcher ``. Create the
204- dispatcher as explained above and pass it to the NGINX client::
208+ dispatcher as explained :ref: `above <HTTP client configuration >` and pass it to
209+ the NGINX client::
205210
206211 use FOS\HttpCache\ProxyClient\Nginx;
207212
@@ -224,7 +229,8 @@ Symfony Client
224229~~~~~~~~~~~~~~
225230
226231The Symfony client sends HTTP requests with the ``HttpDispatcher ``. Create the
227- dispatcher as explained above and pass it to the Symfony client::
232+ dispatcher as explained :ref: `above <HTTP client configuration >` and pass it to
233+ the Symfony client::
228234
229235 use FOS\HttpCache\ProxyClient\Symfony;
230236
@@ -296,6 +302,74 @@ And adapt your bootstrapping code to use the cache kernel::
296302 $response = $cacheKernel->handle($request);
297303 ...
298304
305+ Cloudflare Client
306+ ~~~~~~~~~~~~~~~~~
307+
308+ .. note ::
309+
310+ Cloudflare does not cache HTML pages by default. To cache them, you need to
311+ enable `custom caching with page rules `_ in the Cloudflare administration
312+ interface.
313+
314+ The Cloudflare client does invalidation requests with the `Cloudflare Purge API `_.
315+
316+ The `Cloudflare `_ client sends HTTP requests with the ``HttpDispatcher ``.
317+ Create the dispatcher as explained :ref: `above <HTTP client configuration >`.
318+ Set the `server ` list to the Cloudflare API `['https://api.cloudflare.com'] `.
319+ Do not specify a base URI. The Cloudflare client does not work with base URIs,
320+ you need to always specify the full URL including domain name.
321+
322+ Then create the Cloudflare client with the dispatcher. You also need to pass
323+ the following options:
324+
325+ * ``authentication_token ``: User API token for authentication against
326+ Cloudflare APIs, requires `Zone.Cache ` Purge permissions.
327+ * ``zone_identifier ``: Identifier for the Cloudflare zone you want to purge the
328+ cache for (see below how to obtain the identifier for your domain).
329+
330+ A full example could look like this::
331+
332+ use FOS\HttpCache\CacheInvalidator;
333+ use FOS\HttpCache\ProxyClient\Cloudflare;
334+ use FOS\HttpCache\ProxyClient\HttpDispatcher;
335+
336+ $options = [
337+ 'authentication_token' => '<user-authentication-token>',
338+ 'zone_identifier' => '<my-zone-identifier>',
339+ ];
340+
341+ $httpDispatcher = new HttpDispatcher(['https://api.cloudflare.com']);
342+ $cloudflare = new Cloudflare($httpDispatcher, $options);
343+ $cacheInvalidator = new CacheInvalidator($cloudflare);
344+
345+ When purging the cache by URL, see the `Cloudflare Purge by URL `_ docs for
346+ information about how Cloudflare purges by URL and what headers you can
347+ pass to a :doc: `invalidatePath() <cache-invalidator >` request to clear the
348+ cache correctly.
349+
350+ You need to always specify the domain to invalidate (the base URI mechanism of
351+ the HttpDispatcher is not available for Cloudflare)::
352+
353+ $cacheInvalidator->invalidatePath('https://example.com/path')->flush();
354+
355+ .. note ::
356+
357+ Cloudflare supports different cache purge methods depending on your account.
358+ All Cloudflare accounts support purging the cache by URL and clearing all
359+ cache items. You need a `Cloudflare Enterprise `_ account to purge by cache
360+ tags.
361+
362+ Zone identifier
363+ ^^^^^^^^^^^^^^^
364+ To find the zone identifier for your domain request this from the API::
365+
366+ curl -X GET "https://api.cloudflare.com/client/v4/zones?name={DOMAIN.COM}" \
367+ -H "Authorization: Bearer {API TOKEN}" \
368+ -H "Content-Type:application/json"
369+
370+ The zone identifier is returned in the ``id `` field of the results and is a
371+ 32-character hexadecimal string.
372+
299373Noop Client
300374~~~~~~~~~~~
301375
@@ -379,3 +453,8 @@ requests.
379453.. _message factory and URI factory : http://php-http.readthedocs.io/en/latest/message/message-factory.html
380454.. _Toflar Psr6Store : https://github.com/Toflar/psr6-symfony-http-cache-store
381455.. _Fastly Purge API : https://docs.fastly.com/api/purge
456+ .. _Cloudflare : https://developers.cloudflare.com/cache/
457+ .. _custom caching with page rules : https://support.cloudflare.com/hc/en-us/articles/360021023712-Best-Practices-Speed-up-your-Site-with-Custom-Caching-via-Cloudflare-Page-Rules
458+ .. _Cloudflare Purge API : https://api.cloudflare.com/#zone-purge-all-files
459+ .. _Cloudflare Enterprise : https://developers.cloudflare.com/cache/how-to/purge-cache#cache-tags-enterprise-only
460+ .. _Cloudflare Purge by URL : https://developers.cloudflare.com/cache/how-to/purge-cache#purge-by-single-file-by-url
0 commit comments