How to use data transfer objects (DTO) in Symfony API application

preview_player
Показать описание
In this #PHP programming tutorial I explain how and why you should use data transfer objects (#DTO) for response in your #Symfony applications.
I demonstrate in real-life example how you can prepare your code to use DTOs. I also share some useful tips:
* how to separate data in DTO for customers and admins using serialization groups
* how to "turn off" processing of specific fields if they are not in use for current serialization group
* how to add virtual property into DTO
In this video I use Symfony 5 with PHP 7.4 but this tutorial is still relevant for older versions of Symfony framework or PHP.

⭐️ Final result you can find here ⭐️

⭐️ Support channel on Patreon ⭐️

⭐️ Resources ⭐️
🔗 Book O'Reilly "Head first design patterns"

Get € 20 on Hetzner Cloud hosting:

Register domain without hassle on GoDaddy

⭐️ TABLE OF CONTENT ⭐️

00:00:00 Introduction
00:02:00 Application structure
00:03:04 Define classes for DTOs
00:05:19 Create transformers
00:08:57 Using transformers in controllers
00:09:39 DTO without corresponding entity
00:11:46 Serialization groups
00:13:51 Improve performance
00:15:49 Virtual property
00:16:35 Transformer validation
Рекомендации по теме
Комментарии
Автор

Man I love this, there is a lack of more advanced programming methods used with PHP and Symfony. Thankyou for all your work, looking forward to watching more of your content

MrWilde
Автор

Thank you very very much for this tutorial. I started building API in symfony and found your channel recently. I find your videos very helpful on the subject. You are good at explaining things and showing real life examples. I hope there is more comming soon :). Thank you.

JanKowalski-dmmt
Автор

Thank you for sharing this. Very helpful! I’m trying this right now in a Laravel with Doctrine integration

RealHomeboy
Автор

Me sirvio mucho ....mill gracias . Lo gracioso del caso es que tuve un problema al final con el respond() y lo termine haciendo con un $this->json()

karolinaguaman
Автор

Thank you for the lesson. I think it very useful.

genkazavr
Автор

Great job man. I really like your way of doing these videos. It is a little bit too fast or my taste, but nothing a 0.5 decrease in speed can't fix. Keep it up. Looking forward to more videos.

auras
Автор

Hi There! Thank you for your informative videos. I’m rookie, trying to get into web development. Any chance you can compile some tutorials how to work with IDE like Docker and Symphony framework. It would help a lot! Thank you so much for your time! Keep up with the great work!

romansenyk
Автор

Hello there, great video i love your logic in solve the issue

khusamalfas
Автор

Hey great tutorial, I just wanna know if it's possible to use DTO alongside NelmioApiDoc!

I have used Groups for a month and two and had the benefit of Nelmio auto docing my controllers/endpoints using annotations, but now I want to use DTOs so I believe have to make the annotations a bit more specific which is a long path to go. any help?

pubsvm
Автор

Excellent explanation, but I have a question: In the example controllers you always retrieve all objects, in case you wanted to have a method to fetch one particullar object (e.g. an Order, wich has more objects inside) how would you menage the Request (assuming is an json request)?

matiasnoriega
Автор

What about using just models (business logic), entities(for persistence) and put serialization groups and the same asserts for validation and traits where class properties are repeated. So when returning data in response, you could just set serialization context? You did exactly that on your DTOs.

I have seen projects where DTOs are present and each one of them drags in with class of builder (not in pattern sense)/factory, and i think that is an overkill. You use transformers for that in your video. If you get your serialization group right i think it is enough, for show, for listing, for mobile app. I got myself in the past, serialization groups and HATEOAs was so heavy, every time build objects and its dependencies three levels deep, this was time for DTO for the mobile application. It was a good solution for legacy.

The same question goes for Request data mapped to Form, do we really need it. Would just use a model and validate that, no? Just a thought. I would be interested what do you think of it :)

renarsdilevka
Автор

How to create DTOs system for external API and fectch data ?

godwin.k
Автор

Isn't that add too much boilerplate code?

Maruf-ztdz
Автор

Иван, я то думал, что ты как трушный программер растолкуешь, как делать dto трансформеры юзая Reflection, а ты все ручками сэтишь, еще и в публичные проперти ... пичаль тоска

sergeypoprygin