How to accept gzip with Symfony HTTP Client

Recently, I wrote an HTTP parser using Symfony HTTP client, and I wanted to save some bandwidth because I was using proxies.

One way to save bandwidth is to fetch compressed responses and then decompress them.

HTTP servers and clients use Gzip/deflate or Brotli.

How to match URL with Symfony Router

Have you ever needed to get a route name based on a given URL in Symfony? It might be a weird use case, but the RouterInterface in Symfony has the match() method that will help you do just that.

How to delete doctrine entity by ID in Symfony

The easiest way to remove an entity in Doctrine ORM is by using the $entityManager->remove($entity) method and then calling $entityManager->flush().

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?

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.

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 use Symfony process component with examples

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

$ composer require symfony/process

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.

Symfony vs Laravel Performance Benchmark

I've decided to test the performance of two popular PHP frameworks - Laravel and Symfony. The results surprised me.

The performance test which I've conducted is quite simple. I've created two projects in Symfony and Laravel with only one page, no database queries, no API calls, and no complex calculations, just one simple page. Then I ran HTTP stress tests using a benchmarking tool against both setups and the results are surprising.

You can check both projects on my GitHub - Symfony / Laravel.

Symfony version: 5.4

Laravel version: 10.10

Did you know that Laravel uses a lot of Symfony components under the hood?

Symfony is known for making really good components used across the whole PHP ecosystem in numerous packages. Most of the Symfony components can be used as a standalone package without the need for Symfony itself, which is quite handy.

For example, the laravel/framework packages uses 11 Symfony components (at the time of writing this post), some of which are symfony/routing, symfony/mailer, symfony/http-foundation etc.

Let's start with the fun part.

How to get parameter in Symfony controller

To get parameters in Symfony controller, you can bind a parameter to a variable and inject it into a controller or service.