Image for post
Image for post

Google Photos is a nice service to store and share your favorite shots. Since it is natively supported by Android, a lot of users are automatically uploading their photos from their phones. Google claims that your photos are stored securely and backed up by their own systems. But there are several reasons to have a local backup of your photos. The most obvious reason is that you can’t connect to the internet. A network interruption would also keep you from accessing your photos in the Google Cloud. Also it might be possible that you can not access your Google Account…

Sometimes you want to configure your Guzzle Client to follow redirects when doing a request, for example when you redirect an http to an https URL. In case of a POST request, Guzzle will lose the POST data.

You have to tell Guzzle to use “strict RFC compliant redirects” which will mean a redirected POST request will result in another POST request instead of the default behavior that will result in a GET request.

From the documentation:

Pass an associative array containing the 'max' key to specify the maximum number of redirects and optionally provide a 'strict' key value to…

The Process Component of Symfony allows to execute a command in a subprocess. This is helpful for example if you want the user to start a long lasting command that shouldn’t block the response without using some kind of message queue.

Nevertheless, when the response is send to the client, the process will automatically be terminated. In this case a SIGTERM signal is send which will cause the running process to stop. To avoid this and keep the process running even if the calling request was ended you simply have to override the “__destruct()” method of the Process class.

<?php…

Sometimes, especially when your application grows huge, you want to add a prefix to your database tables to group them according to their domain. This can be done automatically using an Event Listener.

Let’s say you want to add a calendar component to your application, having entities named “configuration”, “appointment” and “detail”. You would create the entities using these names as table names, but the event listener would automatically add the “calendar” prefix in front, resulting in “calendar_configuration”, “calendar_appointment” and “calendar_detail” which improves the readability of your database structure but wouldn’t add additional complexity or naming conventions when writing your…

Image for post
Image for post
One of my aquascapes representing a tree in front of a mountain

Aquascaping is the art of building landscapes in an aquarium. Different to the classical fish tank the focus in aquascaping is more on arranging stones and wood (called “hardscape”) and the plants (called “softscape”). The idea is to rebuild a small scene out of the real world — like a single tree on a clearing of a forest — but in a tiny format and under water using stones, wood, moss and aquatic plants. What makes aquascaping quite popular nowadays is the fact that you can start with relatively small and simple setups that do not need a lot of…

Enjoying a good cup of espresso has become one of my daily rituals to give me an energy boost and to reset my mind to concentrate on something new or getting things done. For many years I enjoyed the average Cappuccino from the well known franchises around. But one day around a year ago I was taken to a small coffee place and had the best Cappuccino I ever had in my life. I wanted to know more about espresso and how I can do something like this at home.

Image for post
Image for post

So after joining a short barista lesson and learning the…

Let’s assume you have a Doctrine Collection which you want to sort by a custom array of IDs.

To sort a Doctrine Collection you have to use an ArrayCollection:

use Doctrine\Common\Collections\ArrayCollection;$result = $this->getEntityManager()
->createQueryBuilder()
->select('e')
->from('MyEntity', 'e')
->getQuery()
->getResult();

$myArrayCollection = new ArrayCollection($result);

The Doctrine ArrayCollection has different methods for accessing the collection as an array. We will use the getIterator() method to create an ArrayIterator and the uasort() method to create a custom comparison function:

// custom array for sorting by id
$myCustomIdArray = [1331,1212,119];
$myArrayCollectionIterator = $myArrayCollection->getIterator();

$myArrayCollectionIterator->uasort(
function ($first, $second) use ($myCustomIdArray) {
foreach ($myCustomIdArray as $myCustomId) {
if ($first->getCustomId() === $myCustomId) {
return -1;
}
if ($second->getCustomId() === $myCustomId) {
return 1;
}
}
// if value is not found in $myCustomIdArray
return 0;
}
);

$myArrayCollectionIterator is now sorted by the values of $myCustomIdArray.

During the development of an application you often face the requirement of instantiating a bunch of services that share the same interface and execute a method in each of it. In general we are then talking about plugins. Let’s assume you have a “Text” object and you want to apply different “Filter” services that implement a “FilterInterface” and have a “execute()” method which will modify the “Text” object in some way.

What you would normally do is to instantiate all Filter services somewhere and executing them one after another. A very simple implementation can look like this:

<?php

namespace
App;

EasyAdmin is a great Symfony bundle for creating administration backends. But one drawback that was always annoying me is that you always have to tell the navigation menu which items should be active. This is only possible by passing the “menuIndex” and “submenuIndex” parameters in the URL. If you want to set a manual link to a specific view or entity you always have to activate the corresponding menu items manually.

My old solution to this was to hard code the URLs in the entity including the menu indexes:

/** * @return string */ public function getProductEditLink(): string { return

KC Müller

Web Developer in Berlin — www.kcmueller.de

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store