DEV Community

loading...
Cover image for Quick 7 tips for Symfony starters

Quick 7 tips for Symfony starters

locastic profile image Locastic ・4 min read

From a version to another reaching 5.2.1 as the latest stable release, Symfony is scaling to become in line with PHP Framework on the throne.

For all of you that have been living under the rock, Symfony is an Open Source PHP Framework for web applications that thousands of web sites and applications rely on. It’s a set of standalone PHP components that most of the leading PHP projects such as Drupal and Laravel use to build their own applications.

For the past few years, Symfony has been the main backend framework for the majority of Locastic projects so I extracted these quick tips from a perspective of a backender with major experience of working on many projects built on top of this growing framework.

1. Make use of others experience

Nowadays, regarding the COVID-19 pandemic, the migration for e-Commerce platforms seems to be an obligation for too many commercial companies and even for small merchants. Once engaged to develop one of these platforms you definitely should use Sylius since it covers almost all e-commerce features and furthermore it’s an Open-Source headless e-commerce platform built on top of Symfony.

For example, take a look at the TommySpiza project that I contributed to and which uses the full potential of the Sylius platform.

2. You don’t need a shotgun to kill an ant

Many times, Symfony developers use FOSUserBundle in an API context to only store users in a database without using the full potential of this bundle.

This is a bad habit of integrating third-party libraries that come with plenty of features and then use only one between them.

So if you are not willing to use functionalities such as login, registration, reset the password, profile and groups just refer to this guide in order to create your custom authentication system with the guard.

3. Never be afraid to try something new

Often, the first thing that came to a Symfony beginner’s mind is that Symfony is an MVC framework! The good news, this is INCORRECT.

Symfony is never defined as an MVC framework but an HTTP framework

Fabien Potencier
From this basis, try to discover the ADR pattern specified by Paul M. Jones, it will amaze you especially with Symfony’s new features such as Autowiring which is an automatic constructor dependency injection system that reduces the configuration when managing services within the container.
Here’s an example of a Mailer service that needs a Logger to operate :

Classic fashion

# app/config/services.yml
services:
logger:
class: App\Service\Logger
mailer:
class: App\Service\Mailer
arguments: ['@logger']

Magic fashion
# app/config/services.yml
services:
mailer:
class: App\Service\Mailer
autowire: true

4. Don’t reinvent the wheel

Surely you’ve heard that while programming it’s discouraged to reinvent the wheel. This really depends on what your aim is. From the perspective of a learning mindset, this seems to be incorrect especially when re-inventing you will learn how solutions are made when resolving problems and you will get a deeper understanding of how they work, but according to a pragmatic mindset, you definitely want to resolve problems at minimal cost and give more importance to the re-use rather than the invention especially when it comes to well-documented existing solutions.

So for example, In order to build a REST API on top of Symfony, you don’t need to spend too much time on purpose. Do it in a faster, cleaner, and more effective way using API Platform Framework, which comes with an easy-to-use and powerful library to create hypermedia-driven REST APIs.

5. Good Developers Write Good Code

The main mission of a software developer is to ship software that works but also which is easily maintainable. To do so, you will usually work with other contributors that may use, maintain or even extend your code so this latter needs to be as clean and understandable as possible to ensure productivity and efficiency. Good code is subjective, so try on building the best code possible. In my opinion, to be a good Symfony developer you must first be able to write good code. For such a purpose try to learn Coding Standards.

6. Think twice, code once, but always test!

There is a knock on the Symfony framework that it is slow when building applications that consume a huge amount of data. So, if you once need to store for example 10.000 records at the same time, you need to figure out how to do it without affecting performance. Batch processing is the best solution for such a use case, just have a look at the bunch of code below.

$batchSize = 20;
for ($i = 1; $i <= 10000; ++$i) {
$symfonista = new Symfonista;
$symfonista->setStatus('newbie');
$symfonista->setUsername('symfonista' . $i);

$em->persist($symfonista);

if (($i % $batchSize) === 0) {
$em->flush();
$em->clear(); // Detaches all objects from Doctrine!
}
}
$em->flush(); //Persist objects that did not make up an entire batch
$em->clear();

This seems to be cool at the first sight, but be careful every new line of code you write is potentially adding new bugs, so don’t forget to write your functional and unit tests.

// inside TestCase class
$entityManagerMock = $this
->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock();
$entityManagerMock
->expects($this->any())
->method('persist')
->will($this->returnValue(true));
$entityManagerMock
->expects($this->any())
->method('flush')
->will($this->returnValue(true));
$entityManagerMock
->expects($this->any())
->method('clear')
->will($this->returnValue(true));

7. Ask an experimenter, don’t ask the doctor

Let’s say you are building a feature estimate. As a junior, no one expects that you know exact numbers, but you can always use the cautious approach – whatever your estimate is, double it! Be careful because there will always be some secret failures inside hidden functionalities you develop and that you should take care of eventually.

Who knows, maybe I’m the experimenter you’d be looking for?
Get in touch if you’re ready to start your Symfony journey or share this article on social media and I’ll jump in for a further discussion.

Discussion (0)

pic
Editor guide