Now that we have done all of the heavy lifting to get our user's posts, we just have a few more lines of code until we can have them show up in the user profile. Let's start by adding some code in our index.php
file to accept the username from the route, pass it to the get_posts_by_user
function, and pass the data to the profile view:
1. Open
index.php
, findthe /user/:username
route, and add the following code to pass the returned posts from ourget_posts_by_user
function to a variable for our view to access:get('/user/:username', function($app) { $app->set('user', User::find_by_username($app- >request('username'))); $app->set('is_current_user', ($app->request('username') == User::current_user() ? true : false)); $app->set('posts', Post::get_posts_by_user($app- >request('username'))); $app->render('user/profile'); });
2. Open
views/user/profile.php
, and add the following code right below the Create a new post text...