backend, Locastic Apr 22

Improve your Symfony proficiency with these tips and tricks!

3 min read –

Whether you’ve been working with Symfony for 10 years or 10 days, you never stop learning new tricks – and that is what makes our job so great! So I decided to share here some of my knowledge of those small Tips & Tricks that you may, or may not, know yet.

Symfony server & local dev

I guess you now all know Symfony Server. This is this little script that avoid you running an Apache or a Nginx. When you run it with symfony serve you can access your app via your browser hitting: http://127.0.0.1:8000(by default) But what you may have missed is that you can access it also from any device in the same network!

Just look for for your internal IP (usually starts with 192.168.x.x it can be found on Linux via ifconfig and ipconfig on Windows) and then, from your mobile phone let’s say, open your browser and hit http://192.168.0.8:8000(that’s my local IP). And now, you can see your amazing local app on your mobile phone. No need to emulate it anymore.

dump in Twig

The dump method is widely known. It is used to print whatever is contained in some variable. Like {{ dump(app.request) }}. But what may not be that much known is that if you run it with {% %} instead of {{ }} it will dump the content in the toolbar instead of directly in the page.

Example of a dump in toolbar on a Sylius 1.7 under Symfony 4.4

Another thing that may be useful when you get thrown in a template you don’t know anything of, is that you can dump all variables, by using whether {{ dump() }} or {% dump %} depending on where you want that displayed. Amazing isn’t it ?

Translating emails

When it comes to emails translation, the only case where everything goes right is when you dispatch the email from the same request as the one that needs it. Like user asks password, we generate email, send it, and create response to serve.

But no-one does that, right ?

When emails do not know the original request anymore, like in Messenger, or if you deal with orders from an admin panel, it may get complicated. Hopefully, Symfony translator does not surrender yet !

You can actually define manually the locale and the domain wanted for the translation. Then you can do things like: {{ 'app.email.order_total'|trans({}, 'messages', order.localeCode) }} assuming you have that information in the order.

default filter

Whenever you want to have some fallback behaviour, ie. go to the route given in URL, or to homepage if none was given, you can either go with some if/else (yuck) or you can use the |default filter !

{% set linkHref = path(givenPath|default('homepage')) %}

The default will use the first variable if possible, and the default one in the following cases:

  • It is a countable (array most often) and has 0 elements
  • It is a Traversable (array also most often) and has 0 elements
  • It is an object and has __toString method but it returns ''
  • It is '' or false or null or []
  • It is not defined at all

Meaning that in some cases, you can stop bothering with all those conditions and have some more concise templates.

peek for Flash messages

When dealing with Flash messages in Twig, you might have noticed that as soon as you read the message, it disappears from the stack. That prevents from doing such thing:

<div class="{% if app.flashes('login-error')|length > 0 %} show{% endif %}">
    {% for message in app.flashes('login-error') %}
        {{ message|trans }}
    {% endfor %}
</div>

If somehow you do want to do it, FlashBag comes with a peek method that will read the message, but not delete it from the stack. This would result in:

<div class="{% if app.session.flashbag.peek('login-error')|length > 0 %} show{% endif %}">
    {% for message in app.flashes('login-error') %}
        {{ message|trans }}
    {% endfor %}
</div>

Great, isn’t it ?

That’s it for this time, but if you also have some tips to share with us, feel free to do so in the comment section. I am quite sure you know some of the hidden tips that only a few people do!