The newsroom moves out

Exercise 2 of 9 in chapter 5 · 100 XP · 3 objectives checked by tests

Topics covered: Static methodSeparation of concernsuse

Odette's nephew did some tidying up overnight. The publications now live in src/Contenu/Redaction.php (Redaction: the newsroom), and each article got a slug, its name in addresses: the-pumpkin-festival. The newspaper's header has moved to src/Web/Page.php. He left you a note: “index.php doesn't need to know everything anymore. It asks the newsroom for the publications, and Page for the layout.”

Since his visit, the preview shows an error: index.php still builds its articles the old way, without a slug, and the new Article constructor requires one. Nothing to repair: all of that code is about to be replaced.

Your mission

  1. In public/index.php, delete the data (the village, the weather report, the authors, the publications array) and all the HTML of the page.
  2. Build the front page in a variable $html: a <main class="articles"> with the versHtml() of each publication from Redaction::publications().
  3. Display it laid out by Page::rendre() (render), with the title 'The Saint-Glinglin Gazette': echo Page::rendre('The Saint-Glinglin Gazette', $html . '</main>');.

Once the cleanup is done, the front page comes back just as it was. Only index.php has slimmed down: about fifteen lines are enough.

The nephew's three classes

They are read-only. Open them to see what they do:

No need to understand every line today. You'll come across two new things in them:

Reminder

A static method (public static function) is called on the class, without creating an object: Redaction::publications(), with :: instead of ->. Putting each responsibility in its own class makes index.php easy to read: all it does now is put things together. That's what will let the next exercise serve several pages.