Bruna invents a dish

Exercise 4 of 6 in chapter 2 · 180 XP · 5 objectives checked by tests

Topics covered: persistSlugger

Bruna has an idea a day, and she's tired of going through SQL in the console. She wants a page to add her dishes: /kitchen/new-dish. The form is ready, and the navigation already leads to it (the “Kitchen” link).

Your mission

In CuisineController::nouveauPlat() (new dish), when the form is submitted (POST):

  1. Read the nom (name), description, prix (price) and specialite (specialty) fields.
  2. Reject invalid data, with a 422 status code and a message:
    • no name → “Give the dish a name.”;
    • a zero or negative price → “The price must be greater than zero.”;
    • a dish already on the menu (same slug) → “This dish is already on the menu.”.
  3. Create the Plat, with a slug made from its name (“Onion Soup Gratinée” → onion-soup-gratinee).
  4. Save it, then redirect to its page.

Reminder

The EntityManager is the guardian of the database. persist() hands it a new object; flush() writes everything that changed, in one transaction. Symfony's slugger turns a text into a URL identifier, accents and apostrophes included.