How to cache Symfony responses with Cloudflare for free

Caching is a great way to speed up your website and reduce your server load. Why waste energy on pages that rarely change?

ElasticSearch fix 406 Not Acceptable: error

I'm trying to index a document with ElasticSearch PHP client (version 8.8), and I'm getting an error when trying to execute the create document request:

406 Not Acceptable: {"error":"Content-Type header [application/vnd.elasticsearch+json; compatible-with=8] is not supported","status":406}

The weird thing is that the client allowed me to create an index, but when I tried to index a document, I got an error...😕

My configuration is:

  • ElasticSearch PHP client: version 8.8
  • ElasticSearch server: version 7.10

How to return JSON response from Symfony Controller

To return JSON response from Symfony Controller, you can use the $this->json(); method or new JsonResponse(['...']);

JSON (JavaScript Object Notation) is a lightweight data interchange format commonly used for transmitting and storing structured data. It consists of key-value pairs and arrays, making it versatile for representing various data types in a human-readable format that can be easily processed by computers. It is widely used in programming and especially in APIs.

Cloudflare API purge cache by URL example

Cloudflare is a great free tool that offers static asset caching out of the box. You can also cache dynamic pages with it, which is quite handy.

Caching things is nice, but invalidating the cache is not a fun business. 

There is this popular quote:

There are only two hard things in Computer Science: cache invalidation and naming things.
-- Phil Karlton

I've done a little experiment with Cloudflare. I've configured caching in my blog. The post you are reading right now is cached in CF, currently with a TTL (Time to live) of 1 day.

After configuring the cache, a question immediately arose in my mind. If I update an article I'll need to wait for the cache to expire before the modifications become visible to the public, which is not ideal. I want immediate updates, but then I also want caching

So what is the solution? Simple - invalidate the cache each time the blog post is updated. Thankfully, Cloudflare offers free API, which allows you to do just that - purge cached content by URL. The documentation is not quite clear, and the method I've used is not documented 🤔, but it works, trust me.

How to use Symfony DomCrawler with examples

The Symfony DomCrawler component is a powerful tool within the Symfony framework for web scraping and HTML/XML parsing. It provides a convenient and intuitive API for traversing and manipulating HTML or XML documents, making extracting specific elements, attributes, and text from web pages easier.

How to fix "You have a new message" iOS notification

In the last few days, I get "You have a new message" Gmail notifications on my iPhone instead of the normal notification with the subject and part of the message itself.

It's quite an annoying issue, given the fact that I haven't changed my notifications settings.

How to use Symfony process component with examples

To use Symfony process, you need first to install the component:

$ composer require symfony/process

How to modify image EXIF metadata - Date Created / Date Modified [Command line tool]

To modify image EXIF metadata - Date Created / Date Modified you can use ExifTool with the following command:

exiftool -AllDates='2021:11:11 13:25:23' my_image.jpg

Where 2021:11:11 13:25:23 is the desired date and my_image.jpg is the filename of the image.

Note that this is a command line tool that has no graphical interface.

Full Exiftool installation guide

You can find step by step tutorial below.

How to generate app secret in Symfony

To generate Symfony app secret (APP_SECRET) env variable, you can use this hash function

$ date | md5

This takes the current date and hashes it with the md5 function.

What's new in PHP 9

PHP 9 will be the next major release of the PHP language.

The release date is still unknown, but we have hints about what's new. By looking into the PHP Wiki RFC section, we can get clues about what is coming next.

In this article, we'll cover the approved/implemented RFC (Request for Comments) and keep it updated with the latest information about PHP 9.