Although our code works just fine, you might have noticed that we have near identical code in /user/:username
and /user/:username/:skip
. We can cut down on code bloat by moving all of the redundant code into a function and calling it from each route. Let's do this so we can stay in the habit of keeping our code clean.
1. Open
index.php
, and create a function calledget_user_profile
that takes$app
as a parameter, and place it above/user/:username
route.function get_user_profile($app) { }
2. Copy the code from
/user/:username/:skip
into this function. But, this time, instead of justpassing $app->request('skip')
, let's check if it exists. If it exists, let's pass it to theget_posts_by_user
function. If it doesn't exist, we'll just pass it0
.function get_user_profile($app) { $app->set('user', User::get_by_username($app-> request('username'))); $app->set('is_current_user', ($app->request('username') == User::current_user...