Installing a router

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

Topics covered: composer requireDependencyvendor/ComposerRouter

The gazette has only one page, and Madame Lou would like each article to have its own, with its own address: /articles/the-pumpkin-festival. To match an address to some code, you need a router. You could write one yourself, but others have already done it very well: FastRoute, by Nikita Popov, one of the developers of PHP itself.

Your mission

  1. In the console, install FastRoute: composer require nikic/fast-route.
  2. Read what Composer prints: the version it chose, composer.json updated, the package placed in vendor/, the autoloader regenerated.
  3. Open composer.json: the package has appeared under require, with its version constraint.

The front page doesn't change yet: the router takes the stage a little later in this chapter.

Reminder

composer require vendor/package adds a dependency to the project in a single command: the line in composer.json, the exact version in composer.lock, the code in vendor/, and the autoloading that goes with it. The constraint ^1.3 accepts version 1.3 and later, up to but not including 2.0: the versions that don't break anything. In this editor, Composer has no network access: it picks from a small catalog of packages prepared in advance.