Debugging a Symfony application: from the error page to the silent bug
Debugging a Symfony application means tracing a symptom back to the faulty code, with the error page, its stack trace and the console commands.
// No error: the message shows, the page redirects… and the database stays empty. if ($form->isSubmitted() && $form->isValid()) { $entityManager->persist($plat); // Missing: $entityManager->flush(); $this->addFlash('success', sprintf('"%s" is on the menu!', $plat->getNom())); return $this->redirectToRoute('app_menu'); }
A loud error shows up on the exception page: the title gives the type and the message, and the stack trace leads to the first file that is not in vendor/. Silent bugs need other witnesses. doctrine:schema:validate spots an inconsistent mapping, dbal:run-sql shows what is really in the database, debug:router, debug:container, debug:event-dispatcher or debug:messenger tell you how Symfony assembled the application, and a command's exit code tells a script what to make of it. The trap: believing that a page that displays is a page that is right. A persist() without flush(), as above, throws nothing; only the database shows that nothing was saved.
The exercises are boss fights: Pipin has "improved" the tavern, and you have to find three or four bugs from the symptoms alone. Along the way: a renamed route, a misnamed relation, a CSRF token that no longer matches, an access_control rule that is too broad, a lost Messenger message, a migration whose rollback empties a table, a badly described workflow and a command that reports a misleading success.
11 exercises work on this concept, by writing code in the browser.
In the tracks
- Class not found Learn PHP 8 from scratch · chapter 3: Too many requires
- 🐉 Boss: trouble in the kitchen Symfony for PHP developers · chapter 1: First steps at the tavern
- 🐉 Boss: the flooded cellar Symfony for PHP developers · chapter 2: The cellar and the pantry
- 🐉 Boss: the cursed scroll Symfony for PHP developers · chapter 3: The tavern's scrolls
- 🐉 Boss: the stolen keys Symfony for PHP developers · chapter 4: The keys to the tavern
- 🐉 Boss: the lost pigeon Symfony for PHP developers · chapter 5: Magic scrolls
- 🐉 Boss: the broken aviary Symfony for PHP developers · chapter 7: The messenger raven
- 🐉 Boss: the collapsed storeroom Symfony for PHP developers · chapter 8: Back to the cellar
- 🐉 Boss: the tavern turned upside down Symfony for PHP developers · chapter 9: The tavern's inner workings
- 🐉 Boss: the misplaced scrolls Symfony for PHP developers · chapter 10: The life of a reservation
- 🐉 Boss: orders misunderstood Symfony for PHP developers · chapter 11: The boss's orders
Related concepts
FormsCSRFaccess_controlrole_hierarchyVoterFirewall#[Groups]Messenger