PHP Symfony Is a great Framework to Create a Web Application

Manuel Kienlein
CodeX
Published in
6 min readAug 4, 2021

--

Photo by Christina @ wocintechchat.com on Unsplash

What is Symfony

Maybe you ask yourself: What actually is Symfony? If you never have heard of it, I want to give you a short introduction:

Symfony is an enterprise web application framework written in PHP. It is organized as a set of reusable PHP components, which makes it very flexible to pick just the required components that you need for your project.
Of course, it’s all open source :)

Who uses Symfony
I did a little research and found out that there are many companies and projects rely on Symfony. According to the Symfony website, StackShare, Twitter and a lot of project websites, I found out that the following companies and software projects are built with Symfony components:
Accenture, Trivago, Typeform, BlaBlaCar, Statista, Deezer, Spotify, Shopware, Magento, Drupal, Joomla, Wikimedia, Matomo, phpBB, ownCloud and much more.

Strength’s of the framework

  • Robust, flexible, well organized and maintainable
  • Long term support
  • Excellent documentation

Basic structure

To give you a quick overview, below is the default folder structure of a typical Symfony application:

symfony_project/
├─ assets/
├─ bin/
│ └─ console
├─ config/
│ ├─ packages/
│ └─ services.yaml
├─ migrations/
├─ public/
│ ├─ build/
│ └─ index.php
├─ src/
│ ├─ Command/
│ ├─ Controller/
│ ├─ DataFixtures/
│ ├─ Entity/
│ ├─ EventSubscriber/
│ ├─ Form/
│ ├─ Repository/
│ ├─ Security/
│ └─ Twig/
├─ templates/
├─ tests/
├─ translations/
├─ var/
│ ├─ cache/
│ └─ log/
└─ vendor/

As you can see, the default folder structure contains all source files in “src”. Web assets are stored in the assets folder. If you use webpack, these will be compiled and stored in the “public/build” folder. The directory “var” contains logging and cache files by default. Also, there are directories for your configuration, templates and translations.

The Model-View-Controller (MVC) is the core architecture.

  • Model
    Database-Objects called “Entities” are stored in the entity folder. The Repository folder contains repositories for these entities to fetch and load objects from the database.
  • View
    The template folder usually contains all of your Twig templates. There you can structure your HTML stuff. In common, you have a file like “base.html.twig” which includes a header and footer for all your pages. Then it is very easy to extend the base with some views that use variables, filters and some presentation logic to create great applications.
  • Controller
    In src/Controller you can define the controllers for your app. Controllers can be accessed via a URL in the browser, which are defined as routes. Controllers contains some logic to glue user inputs with some logic to the database and the view.

Core Features of Symfony

  • MVC Architecture
  • Database Abstraction Layer
  • Template Engine
  • Dependency Injection
  • Event Dispatcher
  • Form Builder
  • Validator
  • Router
  • Caching
  • Security Features
  • Flexible and extendable by Bundles
  • Components for Email, Config, HttpClient and much more
  • Testing Framework

Highlights of Symfony

Below, I created a list with of my six favourite features of the Symfony Framework:

Database Abstraction Layer (Doctrine ORM)

Symfony uses Doctrine as it’s default Object-Relational-Mapper. It allows you to map database tables to object, without writing a single line of SQL. You can use Annotations, XML or YAML for your configuration. The example below shows a class with annotations for contact form entries. Every form input creates a new object, which will be persisted to an MySQL database. It is very easy to store and fetch entries. If you want more performance or more complex queries. You can define them in so-called “Repositories”.

Example of mapping a contact form entity with doctrine annotations

Template Engine (Twig)

Twig is a fast and flexible template engine. Twig supports inheritance and filters. This makes it a powerful and easy tool to create server-side rendered HTML code. The syntax is very similar to Python’s Django.

Example of a Twig file with inheritance, blocks and translations

Translations

Translating the application into several languages is very easy. String translations can be added in several file formats. The most popular are YAML, XLF, JSON, INI, PO and PHP.
The translations can be used in Twig templates easily. Symfony also provides some commands to find missing translations.

Profiler Toolbar

Symfony provides a debugging toolbar called “Profiler”. The toolbar is very useful, if you want to debug your application. It gives you a glimpse of what’s going on under the hood. For example, what headers are sent, cache efficiency, executed database queries, performance insights and much more.

Debugging Toolbar at the bottom of the website, if debugging mode is active

If you want to get more information about your application, you can open the profiler. It is very useful for debugging. It shows you details about your request, session information, form input and exceptions. Also events, cache requests, missing translations, performance and a lot more things are available.

Symfony Profiler shows performance metrics for a given request

Commands

Symfony provides a lot of commands to manage your application. You can clear the cache, perform a database migration, or scan your templates for missing translations.

Example: Command for clearing the cache of Symfony

You can also create your own commands. This is great, because you can use the command-line interface to manage your application.

Router

The Symfony router allows you to map a URL to your controller. There is also a lot of flexibility. You can use annotations, YAML, XML to define your routes. You can use parameters to provide options in your URLs. It is very easy to define which language to use for the specific URLs. The implementation of access control is also possible in the configuration files. Symfony also provides some commands for debugging. It shows you which path and method will be handed to a given controller.

Testing framework (PHPUnit)

Writing tests for your application is a crucial step to create a reliable and secure application. Unit tests can effortlessly be integrated to Symfony. Also, the framework provides components to create whole functional application tests.

Configuration

The application configuration allows you to configure almost everything in your preferred file format. There are default YAML configurations for database, cache, translation, routing, templates and much more.

Extensions (Bundle system)

The bundle system allows you to create and install plugins for your application. Bundles can be installed via composer. It is a package manager for PHP. The configuration takes place in your config file directory.

Conclusion

I am not claiming that PHP Symfony is the best solution for everything. There area a lot of other great frameworks and programming languages out there as well. It depends on what you want to do and what technologies you are familiar with.

Instead, I want to share my fascination about this framework. Symfony is a powerful enterprise framework with lots of great stuff under the hood.
Also, some developers call PHP an “ugly” language. Hopefully I was able to show that using a framework makes your code more structured and “beautiful”.
Finally, I hope that this article has given you a short introduction about Symfony. Maybe you want to use it for your next web project :-).

So, what do you think about Symfony? Do you use it?
Are you considering to give it a try?

Let me know in the comments and consider hitting that ♥ button below to spread it in the world.

--

--

Manuel Kienlein
CodeX

Software Engineer, CS student and a passionate programmer; www.ulinky.net