Forest header image

Symfony Finland
Random things on PHP, Symfony and web development

It's time to get creative with the Symfony Expression Language

Way back in November 2014 the Symfony team introduced the ExpressionLanguage component. It is essentially a simplified version of control structures that you use in the Twig templating language, producing a single value in the end.

The feature has a number of uses, but seems to be under utilized by the Symfony and PHP communities - especially in higher level functionalities. Currently there are 284 dependents on the component on Packagist.

This is a large number, but I've yet to see projects using the expression language in application implementations. I think there is a lot of room for developers to adopt the component in many different functionalities, not only for routing, access control and so on.

Expression Languages are for decision making

Symfony is favored by businesses (small and large) because of it's stability and long term prospects. This is not unlike the reasons why Java continues to be popular. Java has had an expression language, Java Unified Expression Language (JUEL) for over a decade.

Expression languages like Symfony ExpressionLanguage, Hoa\Ruler and JUEL are safe to use and easy to read. They are used in a lot of different contexts, such as JUEL in the Apache Camel Integration Platform configuration files.

The Hoa Project docs offer a good description of writing rules to make decisions:

This is important to understand that rules must be written in a dedicated language. Nevertheless, the way we use rules is very vast and unpredictable. This is why it is primordial to have flexible and extensible rules in the syntax.

For instance, it should be allowed to add operators and functions: “All the customers from the hotel with a Gold pass will receive a voucher of 10%”. The “Gold pass” can be an operator or a function specific to the current business.

- Hack book of Hoa\Ruler

The stability of the Symfony ExpressionLanguage and familiarity with Twig syntax enables many PHP developers to hand over control of certain logic (such as complex eCommerce discount or shipping rules) to people fluent in the ExpressionLanguage syntax.

Expressions in practise

Consider the following snippet that allows validation of sanity of entered user data by checking the age and making sure the country is not in a known list of make believe countries:

The expression is quite easy to read and could be updated by a semi-technical person using an interface (and validation). Confidence in the syntax and security is equivalent to that of Twig and infinitely safer than using PHP's eval function for code execution.

The next time you start solving a problem - consider if ExpressionLanguage could work for you. It's definitely good to learn the basics of using it in the meanwhile.

Written by Jani Tarvainen on Saturday March 19, 2016
Permalink - Tags: symfony, php

« Developer platforms built with the Symfony Framework - Exploring Content API Options »