Summary
Hello, hello,
How are things around here? Not too cold?
This week, we're diving back into the world of Symfony with a short article on bundles.
We're going to take a look at the bundles you need to know when you're a Symfony developer.
The idea here is to bring together the bundles I see and use most often in my projects.
Here, we won't really be talking about bundles that are part of the Symfony "core", such as the "DoctrineMigrationbundle", which you'll find everywhere.
Are you ready? Let's get started!
In this list, we're going to take the bundles that are most common at the time of writing.
So this one's everywhere, on every small website project, even in small and medium-sized businesses? Have you ever spent hours creating the back office interface of the site or application you're working on?
In version 3 EasyAdminBundle was already crazy, but in version 4 it's even crazier.
Version 4, for example, lets you manage file uploads. Instead of configuring everything in a YAML file, this will be done in controllers.
The first controller you'll need to create is the "DashboardController".
This will enable you to customize the "base" of your administration interface.
For example, via the DashboardController, you can add your logo, your name... etc.
Then, for each entity you wish to manage, you'll need to create a controller that will inherit from "AbstractCrudController", in which you'll define: which fields to edit and where to display them, or custom "actions"... etc.
A great Symfony classic... ?
As a developer, your job is to create code... but your code always interacts with data. In production, it's easy, it's the real data, but in dev, it's more complicated.
That's where DoctrineFixturesBundle comes in.
It lets you create fixtures, i.e. test data, which will be inserted into your database.
This will enable you to test your code, without having to create data by hand... well, if in fact you're going to code classes that add data, but.... with a library like Faker, you'll be able to generate random data, so you can concentrate on your core business: coding.
Please note, however, that Faker is no longer maintained.
Heavy stuff again!
Are there any repetitive tasks you're tired of writing? I don't know, for example...
Recording the creation date and modification date of an entity isn't bad, is it?
Or how about an automatic slug for your entities?
This is what the "doctrines extensions" do, and this bundle makes them available in Symfony.
It works very simply: in a configuration file, you can choose the extensions you want to use.
Then, in your entities, all you have to do is import the annotations (attributes, for PHP 8+), and off you go.
In some cases, it's not crazy, I'm thinking, for example, of translatable, which on a query where you have a lot of translated content, will make a lot of SQL queries.
That's why I like KNPDoctrineBehaviorsBundle, which is more optimized for this use, but on the other hand, it doesn't make a "unique" slug.
I've also created my own translation system, in projects where it's the "heart" of the application.
This one has "functions in common" with EasyAdminBundle... ?
First of all, what's it for?
As its name suggests, to manage file uploads. It's very simple: in your entities, you have fields dedicated to this.
Then, it takes care of placing them where they need to be on disk, and of course, if you're using a PaaS, like Clever Cloud, it will let you put them on the AWS-compatible bucket.
Nothing special to say, I haven't used it for a while, because on recent projects, I use EasyAdminBundle's image field.
Nevertheless, when you want to upload files other than in the backoffice, it's essential.
The end of images that weigh a ton?
On many sites, images are resized by the CSS code on the front end.
The problem? The problem is that the browser will download the entire image, then resize it.
This means downloading a very large image for very little money, which is not only bad for the planet, but also gives the user a mediocre experience.
On this site, for example, you'll find the same images several times, in categories, in articles... on the home page.
Except that they don't have the same dimensions on each of these pages, so instead of giving them to the browser as they are, they are processed on the server side, cached and then distributed.
And now, each image has the right size for its location... ain't life grand?
And there's another thing: a JPEG can be optimized, for example with "progressive JPEG", which allows you to start displaying the image in blur while it's loading (and not from top to bottom).
LiipImagineBundle perfectly supports optimization with JPEGOptim, just tell it where the JPEGOptim binary is, give it a little TMPFS space for its temporary files and let it optimize your images for you, all via the LiipImagineBundle config.
And to go even further?
JPEG is aging, and today we find webp, which is lighter, supports transparency ... and so on.
(There's also AVIF, but not yet supported at the time of writing).
WEBP is perfectly handled by LiipImagineBundle, you just tell it to convert in your filter, then with the HTML5 tag, you can tell browsers "where to find the file", and ... voilà.
On this site, for example, all the images your little browser loads are in WEBP, thank you, LiipImagineBundle.
Shall we have a look at the doc?
Connection with social networks... simple.
I used to use "hwi" a lot, but I sometimes use knpuniversity/oauth2-client-bundle, as it seems to be a little better maintained.
Nothing special to say, you just need to have an application (id client and secret client) on the social network you want to use, and you'll be able to connect with it.
This doesn't just work with social networks, but also with corporate SSO servers, if they respect the OAuth2 protocol.
Please note that some SourceOwners are out of date, so you may need to re-implement yours?
Oh, how HORIOUS it is to write a password reset form...
(What do you mean? I've been traumatized? No, it's just NOT interesting... ?)
Ah ... but actually, you don't have to, it's already done, and what's more, it's well done.
It's a simple bundle: import, configure, adapt twigs and enjoy life.
Our users can be as absent-minded as they like and forget their passwords (what do you mean, not everyone uses a password manager?), and we can send them an e-mail so they can reset their passwords.
Ain't life grand? Another annoying thing done for us, thanks symfonycasts/reset-password-bundle.
I have to admit that this one makes me smile, because it reminds me of the naivety of some collaborators on certain projects.
"But nonnnn, people don't make typing errors in their e-mails, you shouldn't see evil everywhere...".
"Yes, yes, if you only knew!
In short, sending e-mails to e-mail addresses that don't exist, or (almost) worse, that don't belong to your users...
This reduces your reputation as an e-mail sender, and what do you think happens next?
Do you end up in the wonderful SPAM folder?
(Next to obscure e-mails making indecent proposals).
Let's fix the problem and check users' e-mail addresses before sending them e-mails.
No need to write it, SymfonyCasts/verify-email-bundle does it for us. ?
Hop hop hop, the doc, here, bizoo.
It's not very exciting to rewrite a pagination system, so we're not going to do it.
(As I'm writing this article, I'm thinking we're lazy bastards, aren't we? ?)
This bundle, you give it your query, and tell it, "I want groups of X items".
That's it, the end.
Even though they'll be very close to the Symfony "core", I'm putting them here, because over time you should come across them more and more.
They allow you to create a user experience, TO THE SMALL ONION, with Symfony.
One of my next applications, currently being deployed in production, uses them.
As well as being a pleasure to dev with, I love the user experience ❤️.
At the heart of this ecosystem is Stimulus, a JS framework.
Do you know why a SAP (single page application) is faster than a traditional "site"?
The main reason is that every time you change "page", your browser reloads (from cache) the CSS and JS (and thus, I'm reinterpreting).
That's where Turbo comes in. It's basically part of the Ruby on Rails ecosystem, but here it is in Symfony.
Rather than reloading the CSS and JS, turbo reloads only the content of the dom... at "page change"... and there's a feeling of fluidity and speed and ding!
(well, that's if your server responds quickly, but if you're hosted at Micheline's... it'll suck).
Turbo, on the other hand, goes one step further with Turbo Frame, and "Turbo Stream"... I'll let you find out what it is... in the turbo doc.
The "basic" autocomplete method
The server generates the JSON -> sends it to the client -> the client transforms this JSON back into HTML -> the client displays the HTML.
In Stimulus (and even Hotwired), HTML is at the center, so
The server generates HTML (with Twig for example) -> sends it to the client -> the client displays the HTML.
How simple!
It's a bit like the previous one, but with a bit more magic.
If you have data that changes often, and you want your client to see it in real time, this is for you.
Create an add-on, update it on the server side and it will be pushed to the client side...
In reality, it's the client that makes an HTTP request every X seconds, which can pose a problem when the load is ramped up...
In this case, take a look at symfony/ux-notify and there you go, it's the server that pushes?
The UX ecosystem has its own site, click here to discover it.
Let's go even further...
Quite simply, Sylius is the "heart" of my first real entrepreneurial project, the Frères Marchand online cheese shop.
Of course, I've added my own layers, but the "core" is Sylius. and frankly...
Well, as usual in the Symfony ecosystem, it works and you don't have to worry about useless questions.
On the online cheese shop, tens of thousands of visitors come to see us every month, and it doesn't budge.
Logistics are completely automated, and Les Frères Marchand receive a clear report of "what to send and to whom".
And all this I was able to do, because Sylius CodeBase is very well done, and very well documented.
Thank you for your help.
Would you like to work on an e-commerce platform too? Here's the official Sylius? website.
But Ludo?
Server-side HTML rendering is fine, but for native iOS and Android applications... what do you do?
Don't worry! We're thinking of everyone in the Symfony ecosystem!
Ever heard of API Platform?
It's a set of bundles that lets you create a REST API in just a few minutes.
Directly in entities, with attributes for the simplest needs...
And always customizable for the most complex.
And, what's more, it generates the API documentation and a client to test it with... who's going to be happy?
It also enables Sylius to be headless... (the good things, eh?!)
The Symfony ecosystem is very rich, and you can save an incredible amount of time on your projects by taking full advantage of it.
The list here is not exhaustive, but I think it's a good place to start.
No, but really... Symfony UX alone?...
It's like all good things, once you've got your teeth into it you think "uh, what was it like before? Don't remember?"
Have a great week and see you next time?