PacktLib: CouchDB and PHP Web Development Beginner's Guide

CouchDB and PHP Web Development Beginner's Guide

Credits

About the Author

About the Reviewers

www.PacktPub.com

Preface

Introduction to CouchDB

The NoSQL database evolution

Introduction to CouchDB

Summary

Setting up your Development Environment

Operating systems

Setting up your web development environment on Mac OS X

Time for action — using Terminal to show hidden files

Time for action — opening your web browser

Time for action — checking your PHP version

Time for action — making sure that Apache can connect to PHP

Time for action — creating a quick info page

Time for action — further configuration of Apache

Time for action — installing Homebrew

Time for action — installing CouchDB

Checking that our setup is complete

Time for action — checking that CouchDB is running

Installing version control

Time for action — installing and configuring Git

Did you have any problems?

Summary

Getting Started with CouchDB and Futon

What is CouchDB?

Time for action — getting a list of all databases in CouchDB

Time for action — creating new databases in CouchDB

Time for action — deleting a database In CouchDB

Time for action — creating a CouchDB document

Futon

Time for action — updating a document in Futon

Time for action — creating a document in Futon

Security

Time for action — taking CouchDB out of Admin Party

Time for action — anonymously accessing the _users database

Time for action — securing the _users database

Time for action — checking to make sure the database is secure

Time for action — accessing a database with security enabled

Summary

Starting your Application

What we'll build in this book

Bones

Project setup

Time for action — creating the directories for Verge

Time for action — initializing a Git repository

Implementing basic routing

Time for action — creating our first file: index.php

Time for action — creating the .htaccess file

Time for action — hooking up our application to Bones

Time for action — creating the class structure of Bones

Time for action — creating functions to access the route on Bones creation

Time for action — creating the register function to match routes

Time for action — creating a get function in our Bones class

Time for action — creating routes for us to test against Bones

Handling layouts and views

Time for action — using constants to get the location of the working directory

Time for action — allowing Bones to store variables and the content path

Time for action — allowing our application to display a view by calling it in index.php

Time for action — creating a simple layout file

Time for action — rendering views inside of our routes

Time for action — creating views

Adding support for other HTTP methods

Time for action — retrieving the HTTP method used in a request

Time for action — altering the register to support different methods

Time for action — adding simple but powerful helpers to Bones

Adding support for complex routing

Adding support for public files

Time for action — altering .htaccess to support public files

Time for action — creating a stylesheet for the application

Publishing your code to GitHub

Get complete code from GitHub

Summary

Connecting your Application to CouchDB

Before we get started

Time for action — creating a database for Verge with curl

Diving in head first

Time for action — adding an e-mail field to the signup form

Time for action — creating a standard object to encode to JSON

Time for action — creating a CouchDB document with PHP and curl

Available CouchDB libraries

Sag

Time for action — using Git to install Sag

Time for action — adding Sag to Bones

Time for action — creating a document with Sag

Time for action — including the classes directory

Time for action — creating a Base object

Time for action — creating a User object

Time for action — plugging the User object in

Wrapping up

Summary

Modeling Users

Before we get started

Time for action — installing Bootstrap locally

Time for action — including Bootstrap and adjusting our layout to work with it

Time for action — sprucing up the home page

Time for action — organizing our user views

Designing our user documents

Time for action — adding the fields to support the user documents

The signup process

Time for action — handling simple user signup

Time for action — cleaning up the signup process

Exception handling and resolving errors

Time for action — examining Apache's log

Time for action : Examine CouchDB's log

Time for action — handling document update conflicts using SagCouchException

Time for action — showing alerts

User authentication

Time for action — adding functionality for users to log in

Time for action – adding functionality for users to log out

Time for action – handling the current user

Summary

User Profiles and Modeling Posts

User profile

Time for action — getting single user documents

Time for action — creating a route for user profiles

Time for action — creating the user profile

Time for action — examining Apache's log

Time for action — handling 500 errors with Bones

Time for action — handling exceptions

Time for action : handling 404 errors with Bones

Time for action — handling 404 errors with Bones

Time for action — checking whether a user is currently logged in

Posts

Creating posts

Time for action — making a function to handle Post creation

Time for action — making a form to enable Post creation

Time for action — creating a route and handling the creation of the Post

Wrapping up

Summary

Using Design Documents for Views and Validation

Design documents

Time for action — creating a temporary view

Time for action — creating a view for listing posts

Time for action — querying the posts_by_user view

Time for action — adding support to get_posts_by_user in the post class

Time for action — adding posts to the user profile

Time for action — creating the reduce function in Futon

Time for action — adding support to our application to consume the reduce function

Time for action — adding support for $_rev to our classes

Time for action — adding support to delete posts in our application

Time for action — adding a validate function to ensure that only creators can update or delete their documents

Time for action – hiding the delete buttons when not on the current user's profile

Wrapping up

Summary

Adding Bells and Whistles to your Application

Adding jQuery to our project

Time for action — adding jQuery to our project

Time for action — creating master.js and connecting Boostrap's JavaScript files

Using jQuery to improve our site

Time for action — improving our user experience by using AJAX to delete posts

Adding simple pagination using jQuery

Time for action — taking posts out of profile.php and putting them in their own partial view

Time for action — adjusting our get_posts_by_user function to skip and limit posts

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

Time for action — adding frontend support for pagination

Time for action — fixing our delete post function to work with pagination

Using Gravatars

Time for action — adding Gravatars to our application

Summary

Deploying your Application

Before we get started

Database hosting with Cloudant

Configuring our project

Time for action — creating a configuration class

Time for action — adding our configuration file to Bones

Application hosting with PHP Fog

Replicating local data to production

Time for action — replicating our local _users database to Cloudant

What's next?

Summary

Pop quiz — Answers

Pop quiz — Answers

Pop quiz — Answers