Serializer

Contents
Normalize objects
Groups control fields
Circular reference note
Dates format

Normalize objects

Normalize turns an object into a plain array: properties become keys, and values stay scalar or nested arrays. No JSON string is produced yet.

That middle step is handy for tweaking data before the answer. Read a field, rename a key, then encode or return the array.

$data = $serializer->normalize($product); $name = $data["name"];

Groups control fields

Groups decide which fields leave the server. Mark each property with the group names it belongs to, then pick groups per call.

Public endpoints expose a small group and admin endpoints a wider one. One entity serves both without extra classes.

$data = $serializer->normalize($product, null, ["groups" => ["product_show"]]);

Circular reference note

Related objects point back at each other: a post knows its author, and the author lists posts. Naive normalization would loop forever.

The limit option stops the loop after a set depth. Real projects often pair it with groups so cycles never start.

$data = $serializer->normalize($post, null, ["circular_reference_limit" => 1]);

Dates format

Dates need one agreed shape across the API. The datetime format option sets it per call without touching the entity.

Use the classic year month day pattern for JSON answers. Frontend code parses one format everywhere.

$data = $serializer->normalize($order, null, ["datetime_format" => "Y-m-d"]);

Next: Security intro

Article author: Arthur Isaev

Related articles
Development with PHP
What is Symfony and where it runs
Install Symfony
Your first page
Routes
Controllers
Twig basics
Twig advanced
Doctrine setup
Entities
Migrations
Repositories
Relations
Forms
Validation
Services and DI
JSON controllers
Serializer
Security intro
Users
Login form
Voters
Console commands
Events
Messenger
Mailer
Cache
Sessions
CSRF
Translations
Dotenv and env
Profiler
Testing
Fixtures
Pagination
File uploads
API Platform in one lesson
DQL
Transactions
Migrations advanced
Security advanced
HTTP client
Scheduler
Webhooks
Asset Mapper
Deployment
Performance
Capstone: blog
Capstone: JSON API
Modern Symfony tour
Hero roadmap: the whole course on one page

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: