In this episode, we're putting everything we've covered about models and database access into practice by creating a user profile page. The main goal here is to allow visitors to click on any user from a list and be taken to their own profile page, where we'll show some data about them.
First, we define a new route in our web routes file that will handle URLs like /users/{id}
— essentially, a dynamic route that accepts the user's unique identifier. We then set up a new controller (the UserController
) to handle these routes and render a user.twig
view.
Once the route and controller are ready, we update our home view to link each user's name to their profile page. We use the user's ID for the URL for simplicity. When a user clicks the link, our controller receives that ID through the route placeholder, fetches the appropriate user from the database using the model's find
method, and passes that user data to the view.
With the data now available in the Twig template, we display details about the user, such as their name. We also talk briefly about what happens if someone tries to access a user that doesn't exist: for now, you'll just see an error, but later we'll learn how to handle this more gracefully (like custom 404 pages).
By the end of the episode, we've built a basic and functional user profile page and set up a workflow you can reuse for any other dynamic, data-driven pages. This is a super useful pattern for almost any web application!