Symfony Live Berlin 2014

In October I went to the SymfonyLive in Berlin, I was pleasantly surprised how high the niveau of the speeches was. It was my first time there, in the years before I was only attending some general fairs like the Internet World or the TYPO3-Bootcamp, where I found some lightweighted CSS-talks :)

But here at SimfonyLive my tech-heart jumped higher, because all the topics seemed quite interesting to me when I read the program. After all there were some better speakers and some worse (as always), but these were my favourite ones:

Thorsten Rinne
He talked about a whole refactoring and redesign of the code and infrastructure of Yatego and how they used Elastic Search for that, very interesting talk

Benjamin Eberlei
How does Facebook roll out or test certain features to a small group of people? Tadaaaah, it’s Feature Flags!

Nils Adermann, creator of composer (thanks for that, you’re a genius)
Nils presented a nice way to asynchronically work on datastreams. Wow, I didn’t know you could go so far with PHP. For example a curl-call to a 1-mio-item-json which never has to be persisted in whole

Dustin Whittle: Scaling PHP
MemCached, Workers, Doctrine-Cache, http-cache: a solid roundtrip to all the performance-approaches in the PHP-world.



Useful tips for symfony2/doctrine optimization

In the LinkedIn-symfony-group I stumbled upon a very useful link:

This is a must-read for all doctrine-beginners, I wish i woud have read it when I started… Here are the points in short:

  • use the Symfony Profiler toolbar
  • avoid object hydration
  • use IDs for referencing rather than whole object
  • use update-statement
  • use lazy collections
  • never load entities in a loop

More infos can be found in the article.

Truncate text in Twig & Symfony 2

In the core symfony / twig do not have a simple truncate-text-function, like substr() in PHP. Therefor you have to activate the Twig-Text-Extension:

Now you can easily truncate as you like:

If you want to preserve whole words, you have to set the 2nd parameter:

With the 3rd parameter you can override the default seperator ‘…':


Thanks Scott :)

Store Symfony-Sessions in the Database

On my recent project I used cloudhosting for easy scaling. Unfortunately they don’t support synchronized filestorage, so when you store a session on one machine, on the next request it is gone :(

There is a quite simple workaround, just store your sessions in the database:

The drawback is a (possibly) pretty huge database, so make sure you have some kind of garbage collector that cleans the entries from time to time.

PHP-Traits in Symfony: Add timestamps and more to tables magically

Say you want to add a logging preprocessor to some of your classes. Either all of these classes have to inherit some kind of logger-class, or you have to copy the logging-call multiple times. Not anymore! PHP 5.4.0 introduced Traits, a wonderful way to inject code into your classes without using inheritance.

A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way.

Some guys in the Symfony-community prepares some great traits:

  • Timestampable
  • Loggable
  • Translatable
  • Sortable
  • Tree

Go check them out:

Trailing slash in Symfony route

I found several approaches to get rid of the annoying 404-error when you open a route with a trailing-slash:
>> ok
>> Error: 404-not found

The easiest solution is to add a RewriteRule to your .htaccess:

Now all requests with trailing-slash are redirected to the one without trailing-slash.

Notice that this only works as long as you do not have any routes which want require a trailing slash, because this would lead to an redirect loop:


Table inheritance with Doctrine

Currently I am programming a web-application with Symfony. In this app we got different kind of users: patients, doctors, experts, admins and so on. All of these users share some attributes (username, password, email), but some attributes only apply to several usertypes. A doctor for example has a departement, an expert a phone-number etc.

In the object oriented world of PHP this is an easy task: Patient extends User. But thanks to the magic of Doctrine this can be used within MySQL as well!

Daniel Barsotti wrote an excellent post about it:

Works like a charm!