Stream an image from Symfony controller

When sending an image/file from a Symfony controller you would usually read the file from filesystem.
This you can do by passing the file path to the BinaryFileResponse class and return it in the controller, as described in the docs:
https://symfony.com/doc/current/components/http_foundation.html#serving-files

use Symfony\Component\HttpFoundation\BinaryFileResponse;

$file = 'path/to/file.txt';
$response = new BinaryFileResponse($file);Code language: PHP (php)

But in some cases you might want to create an image dynamically and return it directly from the controller without storing it on the filesystem.
So you can create your image with the GD library and directly stream the GDImage object from the symfony controller with the StreamedResponse class.

Continue reading “Stream an image from Symfony controller”

Symfony deprecation log channel

Are you annoyed of too many deprecation warnings in you logs of your symfony app?
Probably yes because it is really a lot of noise.
However deprecation logs are still useful to ensure future compatibility of your app.

So since version 5.1 symfony will log deprecations to a dedicated log channel when it exists and ships with this monolog config:

monolog:
    channels:
        - deprecation # Deprecations are logged in the dedicated "deprecation" channel when it existsCode language: PHP (php)

This is added already in the recipe and ships when installing symfony.

Ok, but the handler for this deprecation channel is not configured, so you have to do this yourself.
How? Add this to your monolog config:

Continue reading “Symfony deprecation log channel”

Converting umlaute with symfony String component

There are multiple PHP native ways to convert umlaute and other special characters to ascii save formats, but most of them i experience insufficient in the one or other way.

Since i am using symfony anyway i can use the String component which has a handy slugger which converts any characters into safe ascii characters.
It is very flexible and can be customized with locales, custom replacements and closures.

$umlautString = "Müller Meier";
$slugger = new Symfony\Component\String\Slugger\AsciiSlugger('de');
$slugger->slug($umlautString, $seperator = ' ')->toString();
echo $umlautString; // mueller meierCode language: PHP (php)

I guess this will become my go-to method resp. slugger to convert umlaute in any PHP application.

Continue reading “Converting umlaute with symfony String component”

Migrating user table from Mysql to Postgres with Symfony and Doctrine

When using bin/console make:entity on Mysql and then later you switch your application to Postgres and you have a table called user, which you most likely have when using security component of Symfony.
Then you will receive an error because user is a reserved word in Postgres!

An exception occurred while executing 'INSERT INTO user (id, email, roles, password, is_verified) VALUES (?, ?, ?, ?, ?)' with params [3, "dev@dev.de", "[]", "your-encrypted-password", 0]:
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "user"
LINE 1: INSERT INTO user (id, email, roles, password, is_verified) V...

Continue reading “Migrating user table from Mysql to Postgres with Symfony and Doctrine”

Passing a boolean to the constructor of “Symfony\Component\Dotenv\Dotenv” is deprecated

You might encounter this deprecation message after your upgrade to symfony 5.1 in your symfony project:

Since symfony/dotenv 5.1: Passing a boolean to the constructor of “Symfony\Component\Dotenv\Dotenv” is deprecated, use “Dotenv::usePutenv()

To fix this, go to config/bootstrap.php and remove the false from the Dotenv constructor:
(new Dotenv(false))->loadEnv(dirname(DIR).'/.env');
to
(new Dotenv())->loadEnv(dirname(DIR).'/.env');
See: https://github.com/symfony/symfony/issues/37195

Actually the file config/bootstrap.php has been removed from symfony 5.1 and was replaced in public/index.php.

But since the project was updated from an symfony 4 version the bootrap file is still present.

So another option would be to remove the boostrap file and update the project entirely to symfony 5.1 recipe.
This is not trivial though as you can see in this corresponfing PullRequest.

Disable symfony deprecation warnings in PHPUnit tests

Symfony’s deprecation warnings while running tests is a great service to keep track with upcoming changes in newer symfony versions.
However these warnings can break your CI/CD pipeline and sometimes you cant fix all deprecation warnings immediatly.

To disable them you can set the ENV var
SYMFONY_DEPRECATIONS_HELPER=disabled
and the warnings will not be displayed anymore and CI/CD will pass again.
Update from comments:

SYMFONY_DEPRECATIONS_HELPER=weak
does also work and will still show the deprecation warnings count. (Thx Max)

Continue reading “Disable symfony deprecation warnings in PHPUnit tests”

Symfony and Angular: shared translations

Angular for frontend with symfony delivering the data have become quite a common setup.

When working with this constellation you will sooner or later come across the i18n topic. Most likely you would want to share translations between front- and backend. So that you could keep translations in one single location while using it for frontend templates as well as server-side error messages etc.

One approach would be to store translations in the symfony yml or xml files and deliver those to the frontend via an api endpoint.
Given that front- and backend-code run on the same server, there is an even simpler solution.
Continue reading “Symfony and Angular: shared translations”

Symfony Cache Component with SncRedisBundle

Since version 3.1 there is a Cache component with a PSR-6 implementation integrated in Symfony.
So we do not need to add a 3rd party lib as Doctrine Cache anymore when we need caching these days.

Doctrine Cache does the job but by implementing a standard the Symfony Cache seems to have another advantage besides its delievered anyway. So i checked it out.

Documentation is still a bit sparse and the best resource so far is this blogpost:
https://symfony.com/blog/new-in-symfony-3-1-cache-component

I wont go into detail on how you use the Cache but rather how you integrate it with Redis, which is my favourite Cache backend.
Continue reading “Symfony Cache Component with SncRedisBundle”