O'Reilly logo

CouchDB and PHP Web Development Beginner's Guide by Tim Juravich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action — refactoring our code so it's not redundant

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 called get_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 just passing $app->request('skip'), let's check if it exists. If it exists, let's pass it to the get_posts_by_user ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required