We are going to create a route so that people can see a profile by going to a unique URL. This will be the first time that we'll really utilize our routing system's ability to handle route variables.
1. Open
index.php
, and create aget
route for a user profile by entering the following code:get('/user/:username', function($app) { });
2. Let's use the route variable
:username
to tell us the username that we want to find; we'll pass this to theget_by_username
function we created in theUser
class. Finally, we'll pass the returneduser
object to the view in theuser
variable:get('/user/:username', function($app) { $app->set('user', User::get_by_username($app- >request('username'))); });
3. Lastly, we'll render the
user/profile.php
view, which we will create shortly.get('/user/:username', function($app) { $app->set('user', User::get_by_username($app- >request('username'))); $app->render('user/profile'); });