Book Image

CouchDB and PHP Web Development Beginner's Guide

By : Tim Juravich
Book Image

CouchDB and PHP Web Development Beginner's Guide

By: Tim Juravich

Overview of this book

CouchDB is a NoSQL database which is making waves in the development world. It's the tool of choice for many PHP developers so they need to understand the robust features of CouchDB and the tools that are available to them.CouchDB and PHP Web Development Beginner's Guide will teach you the basics and fundamentals of using CouchDB within a project. You will learn how to build an application from beginning to end, learning the difference between the "quick way"ù to do things, and the "right way"ù by looking through a variety of code examples and real world scenarios. You will start with a walkthrough of setting up a sound development environment and then learn to create a variety of documents manually and programmatically. You will also learn how to manage their source control with Git and keep track of their progress. With each new concept, such as adding users and posts to your application, the author will take you through code step-by-step and explain how to use CouchDB's robust features. Finally, you will learn how to easily deploy your application and how to use simple replication to scale your application.
Table of Contents (17 chapters)
CouchDB and PHP Web Development Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
4
Starting your Application

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. 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. 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 function. If it doesn't exist, we'll just pass it 0.

    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...