The slug that writes itself

Exercise 3 of 6 in chapter 9 · 270 XP · 5 objectives checked by tests

Topics covered: Doctrine listeners#[AsEntityListener]prePersistSluggerInterface

A dish's slug (dwarf-stew, in /dish/dwarf-stew) used to be computed in the kitchen controller. But dishes also arrive through the console commands, the Friday feast, the tests… and every time, someone has to remember the slug. Pippin forgot once: dish saved, SQL error, evening ruined.

So he removed the computation from the controller. From now on, Doctrine takes care of it.

Doctrine events

Doctrine also announces what it does: prePersist (just before inserting an entity), preUpdate, postRemove… An entity listener reacts to these events for one specific class. The slug is then guaranteed, wherever the dish comes from.

Your mission

Complete SlugDuPlat (the dish's slug):

  1. a dish saved without a slug gets one, made from its name (“Apple Pie” gives apple-pie);
  2. accents and punctuation disappear (“Crème Brûlée with Phœnix Egg” gives creme-brulee-with-phoenix-egg);
  3. a hand-picked slug is kept: those on the menu (dwarf-stew) do not change;
  4. two dishes never share a slug: ragout, then ragout-2, then ragout-3;
  5. the kitchen works again.

To try it: log in with bruna@drunken-dragon.me / cauldron and invent a dish.