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
- In
public/index.php, delete the data (the village, the weather report, the authors, the publications array) and all the HTML of the page. - Build the front page in a variable
$html: a<main class="articles">with theversHtml()of each publication fromRedaction::publications(). - 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:
Redactionkeeps what the gazette has published.Redaction::publications()returns the articles and the classified ad.Pagebuilds the complete page.Page::rendre($titre, $contenu)wraps your content with the newspaper's header, the stylesheet and the weather report.Urlbuilds the site's addresses. You'll be using it soon.
No need to understand every line today. You'll come across two new things in them:
- a heredoc in
Page:<<<HTMLopens a long string over several lines, which ends at the wordHTML;. Just like between double quotes,$variablesare replaced by their values; self::inRedaction:self::publications()is the class calling one of its own static methods.
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.