Serializer
| 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