Chapter 6 of the Learn PHP 8 from scratch track
The Gazette is online
7 exercises · 1,200 XP · about 3 h
Template<?= ?>ob_start()extract()requireAlternative syntaxGetterLayoutEnum::cases()FormsPOST method$_POSTValidation422XSShtmlspecialchars()is_stringGET method$_GETQuery stringstr_contains()array_filter()ControllerCallable::classDynamic instantiation
The chapter's exercises
-
A first template
Exercise 1 · 150 XP · 3 objectives checked by tests
Madame Lou wants to touch up the “Lost in the post” page, but its HTML is buried in concatenations: '<p><a class="retour" href="' . Url::vers('/') . '">'… A template (gabarit in French) turns things around: it is a file of HTML, with just enough PHP to slip the values in. First you need the tool that fills it in: a Vue (view) class.
Template<?= ?>ob_start()extract()require
-
The article template
Exercise 2 · 150 XP · 2 objectives checked by tests
The “Lost in the post” page has its template, and Madame Lou wants one for an article's page: its HTML is still buried in the concatenations of versHtmlComplet() (full HTML). This one needs a little more PHP, because the photo is only displayed if the article has one.
Alternative syntaxGetter
-
A shared layout
Exercise 3 · 150 XP · 4 objectives checked by tests
One big block of HTML is still stuck inside PHP: the layout in Page::rendre(), squeezed into a heredoc. It deserves its own template. And while we're at it, Gaston wants a menu to go straight to the weather.
TemplateLayoutEnum::cases()
-
Posting a classified ad
Exercise 4 · 200 XP · 5 objectives checked by tests
Madame Lou is drowning in little notes slipped into her mailbox: “Wheelbarrow for sale”, “Looking for a ginger cat”… From now on, the villagers will post their classified ads online. Odette's nephew has already designed the form, in templates/proposer-annonce.php (submit an ad): it expects $valeurs (values), what was typed, and $erreurs (errors), the list of problems.
FormsPOST method$_POSTValidation422
-
No traps in the ads
Exercise 5 · 200 XP · 4 objectives checked by tests
Léo has found a new game. In the title of an ad, he typed "><script>alert('Léo')</script> and left the contact empty. The form came back with its errors… and a “Léo” pop-up appeared: his text had become code, run by the browser. This is an XSS vulnerability (cross-site scripting). With a real script, Léo could make someone else's page do anything at all, just by sending them a booby-trapped link or form.
XSShtmlspecialchars()Validationis_string
-
Searching the gazette
Exercise 6 · 150 XP · 5 objectives checked by tests
Odette can't find the article about the soup contest anymore, and she doesn't feel like going through every section. The gazette needs a search: a field in the masthead, and a page that displays the articles whose title contains the word typed.
GET method$_GETQuery stringstr_contains()array_filter()
-
Controllers as classes
Exercise 7 · 200 XP · 3 objectives checked by tests
public/index.php is overflowing: five routes, five anonymous functions, and more on the way. Madame Lou asks for one last tidy-up before going live: one controller per page, a class in src/Controleur/ (controller). The front controller will only match addresses to controllers.
ControllerCallable::classDynamic instantiation