Using route parameters
In the previous recipe, we defined a route and controller that responded to the /hello-world
path. A route can add variables to the path, called route parameters, so that the controller can handle different URLs.
In this recipe, we will create a route that takes a user ID as a route parameter to display information about that user.
Getting started
This recipe uses the route created in the Defining a controller recipe to provide a custom page.
How to do it…
- First, remove the
src/Routing/RouteSubscriber.php
file andmymodule.services.yml
from the previous section and clear the Drupal cache, so it does not interfere with what we are about to do. - Next, edit
routing.yml
so that we can add a route parameter ofuser
to the path:path: /hello-world/{user}
Route parameters are wrapped with an opening bracket ({
) and a closing bracket (}
).
- Next, we will update the requirements key to specify that the
user
parameter...