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

Related concepts

FormsCSRFaccess_controlrole_hierarchyVoterFirewall#[Groups]Messenger