You are previewing CouchDB and PHP Web Development Beginner's Guide.
O'Reilly logo
CouchDB and PHP Web Development Beginner's Guide

Book Description

Get your PHP application from conception to deployment by leveraging CouchDB’s robust features with this book and ebook.

  • Build and deploy a flexible Social Networking application using PHP and leveraging key features of CouchDB to do the heavy lifting

  • Explore the features and functionality of CouchDB, by taking a deep look into Documents, Views, Replication, and much more.

  • Conceptualize a lightweight PHP framework from scratch and write code that can easily port to other frameworks

  • In Detail

    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

    1. CouchDB and PHP Web Development Beginner's Guide
      1. Table of Contents
      2. CouchDB and PHP Web Development Beginner's Guide
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Time for action — heading
          1. What just happened?
          2. Pop quiz — heading
          3. Have a go hero — heading
        6. Reader feedback
        7. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Introduction to CouchDB
        1. The NoSQL database evolution
          1. What makes NoSQL different
            1. Classification of NoSQL databases
            2. CAP theorem
            3. ACID
            4. So what does all of that mean?
              1. Advantages of NoSQL databases
              2. Negatives of NoSQL databases
              3. When you should use NoSQL databases
              4. When you should avoid NoSQL databases
        2. Introduction to CouchDB
          1. The history of CouchDB
          2. Defining CouchDB
        3. Summary
      9. 2. Setting up your Development Environment
        1. Operating systems
          1. Windows
            1. Installing Apache and PHP
            2. Installing Git
            3. Installing CouchDB
          2. Linux
            1. Installing Apache and PHP
            2. Installing Git
            3. Installing CouchDB
        2. Setting up your web development environment on Mac OS X
          1. Terminal
        3. Time for action — using Terminal to show hidden files
          1. What just happened?
          2. Text editor
          3. Apache
          4. Web browser
        4. Time for action — opening your web browser
          1. What just happened?
          2. PHP
        5. Time for action — checking your PHP version
          1. What just happened?
        6. Time for action — making sure that Apache can connect to PHP
          1. What just happened?
        7. Time for action — creating a quick info page
          1. What just happened?
          2. Fine tuning Apache
        8. Time for action — further configuration of Apache
          1. What just happened?
          2. Our web development setup is complete!
          3. Installing CouchDB
          4. Homebrew
        9. Time for action — installing Homebrew
          1. What just happened?
        10. Time for action — installing CouchDB
          1. What just happened?
        11. Checking that our setup is complete
          1. Starting CouchDB
        12. Time for action — checking that CouchDB is running
          1. What just happened?
          2. Running CouchDB as a background process
        13. Installing version control
          1. Git
        14. Time for action — installing and configuring Git
          1. What just happened?
        15. Did you have any problems?
          1. Pop quiz
        16. Summary
      10. 3. Getting Started with CouchDB and Futon
        1. What is CouchDB?
          1. Database server
          2. Documents
            1. Example of a CouchDB document
            2. JSON format
            3. Key-value storage
            4. Reserved fields
          3. RESTful JSON API
        2. Time for action — getting a list of all databases in CouchDB
          1. What just happened?
        3. Time for action — creating new databases in CouchDB
          1. What just happened?
        4. Time for action — deleting a database In CouchDB
          1. What just happened?
        5. Time for action — creating a CouchDB document
          1. What just happened?
        6. Futon
        7. Time for action — updating a document in Futon
          1. What just happened?
        8. Time for action — creating a document in Futon
          1. What just happened?
        9. Security
        10. Time for action — taking CouchDB out of Admin Party
          1. What just happened?
        11. Time for action — anonymously accessing the _users database
          1. What just happened?
        12. Time for action — securing the _users database
          1. What just happened?
        13. Time for action — checking to make sure the database is secure
          1. What just happened?
        14. Time for action — accessing a database with security enabled
          1. What just happened?
          2. Pop quiz
        15. Summary
      11. 4. Starting your Application
        1. What we'll build in this book
        2. Bones
        3. Project setup
        4. Time for action — creating the directories for Verge
          1. What just happened?
          2. Source control with Git
        5. Time for action — initializing a Git repository
          1. What just happened?
        6. Implementing basic routing
        7. Time for action — creating our first file: index.php
          1. What just happened?
          2. .htaccess files
        8. Time for action — creating the .htaccess file
          1. What just happened?
          2. Hacking together URLs
          3. Creating the skeleton of Bones
        9. Time for action — hooking up our application to Bones
          1. What just happened?
          2. Using Bones to handle requests
        10. Time for action — creating the class structure of Bones
          1. What just happened?
            1. Accessing the route
        11. Time for action — creating functions to access the route on Bones creation
          1. What just happened?
            1. Matching URLs
        12. Time for action — creating the register function to match routes
          1. What just happened?
          2. Calling the register function from our application
        13. Time for action — creating a get function in our Bones class
          1. What just happened?
            1. Adding routes to our application
        14. Time for action — creating routes for us to test against Bones
          1. What just happened?
          2. Testing it out!
          3. Adding changes to Git
        15. Handling layouts and views
          1. Using Bones to support views and layouts
        16. Time for action — using constants to get the location of the working directory
          1. What just happened?
        17. Time for action — allowing Bones to store variables and the content path
          1. What just happened?
        18. Time for action — allowing our application to display a view by calling it in index.php
          1. What just happened?
        19. Time for action — creating a simple layout file
          1. What just happened?
          2. Adding views to our application
        20. Time for action — rendering views inside of our routes
          1. What just happened?
        21. Time for action — creating views
          1. What just happened?
          2. Adding changes to Git
        22. Adding support for other HTTP methods
        23. Time for action — retrieving the HTTP method used in a request
          1. What just happened?
        24. Time for action — altering the register to support different methods
          1. What just happened?
        25. Time for action — adding simple but powerful helpers to Bones
          1. What just happened?
          2. Using a form to test our HTTP method support
          3. Testing it out!
          4. Adding changes to Git
        26. Adding support for complex routing
          1. Handling complex routes
          2. Accessing route variables
          3. Adding more complex routes to index.php
          4. Testing it out!
          5. Adding changes to Git
        27. Adding support for public files
        28. Time for action — altering .htaccess to support public files
          1. What just happened?
        29. Time for action — creating a stylesheet for the application
          1. What just happened?
          2. Adding changes to Git
        30. Publishing your code to GitHub
        31. Get complete code from GitHub
        32. Summary
      12. 5. Connecting your Application to CouchDB
        1. Before we get started
        2. Time for action — creating a database for Verge with curl
          1. What just happened?
        3. Diving in head first
          1. Adding logic to our signup script
        4. Time for action — adding an e-mail field to the signup form
          1. What just happened?
            1. Using curl calls to post data to CouchDB
        5. Time for action — creating a standard object to encode to JSON
          1. What just happened?
            1. Committing it to Git
        6. Time for action — creating a CouchDB document with PHP and curl
          1. What just happened?
            1. Committing it to Git
          2. Is this technique good enough?
        7. Available CouchDB libraries
        8. Sag
          1. Downloading and setting up Sag
        9. Time for action — using Git to install Sag
          1. What just happened?
            1. Adding Sag to Bones
        10. Time for action — adding Sag to Bones
          1. What just happened?
          2. Simplifying our code with Sag
        11. Time for action — creating a document with Sag
          1. What just happened?
          2. Adding more structure
        12. Time for action — including the classes directory
          1. What just happened?
            1. Working with classes
        13. Time for action — creating a Base object
          1. What just happened?
        14. Time for action — creating a User object
          1. What just happened?
        15. Time for action — plugging the User object in
          1. What just happened?
            1. Testing it out
            2. Committing it to Git
        16. Wrapping up
        17. Summary
      13. 6. Modeling Users
        1. Before we get started
          1. Cleaning up our interface by installing Bootstrap
        2. Time for action — installing Bootstrap locally
          1. What just happened?
        3. Time for action — including Bootstrap and adjusting our layout to work with it
          1. What just happened?
        4. Time for action — sprucing up the home page
          1. What just happened?
          2. Moving all user files into the user folder
        5. Time for action — organizing our user views
          1. What just happened?
        6. Designing our user documents
          1. How CouchDB looks at basic user documents
          2. Adding more fields to the user document
            1. Discussing options for adding these fields
            2. Adding support for the additional fields
        7. Time for action — adding the fields to support the user documents
          1. What just happened?
        8. The signup process
          1. A little administrator setup
          2. Updating the interface
          3. Quick and dirty signup
        9. Time for action — handling simple user signup
          1. What just happened?
          2. SHA-1
            1. Testing the signup process again
          3. Refactoring the signup process
        10. Time for action — cleaning up the signup process
          1. What just happened?
        11. Exception handling and resolving errors
          1. Deciphering error logs
        12. Time for action — examining Apache's log
          1. What just happened?
        13. Time for action : Examine CouchDB's log
          1. What just happened?
          2. Catching errors
        14. Time for action — handling document update conflicts using SagCouchException
          1. What just happened?
          2. Showing alerts
        15. Time for action — showing alerts
          1. What just happened?
        16. User authentication
          1. Setting up for the login form
          2. Have a go hero — setting up the routes and forms for login
          3. Logging in and logging out
        17. Time for action — adding functionality for users to log in
          1. What just happened?
        18. Time for action – adding functionality for users to log out
          1. What just happened?
          2. Handling the current user
        19. Time for action – handling the current user
          1. What just happened?
        20. Summary
      14. 7. User Profiles and Modeling Posts
        1. User profile
          1. Finding a user with routes
        2. Time for action — getting single user documents
          1. What just happened?
        3. Time for action — creating a route for user profiles
          1. What just happened?
        4. Time for action — creating the user profile
          1. What just happened?
            1. Testing it out
            2. Adding your changes to Git
          2. Fixing some problems
            1. Finding errors
        5. Time for action — examining Apache's log
          1. What just happened?
          2. Handling 500 errors
        6. Time for action — handling 500 errors with Bones
          1. What just happened?
        7. Time for action — handling exceptions
          1. What just happened?
            1. Testing our exception handler
          2. Showing 404 errors
            1. 404 if user isn't found
        8. Time for action : handling 404 errors with Bones
          1. What just happened?
            1. Showing 404 errors for unknown users
            2. Hooking up 404 all around the site
        9. Time for action — handling 404 errors with Bones
          1. What just happened?
            1. Testing it out
          2. Giving users a link to their profile
          3. Creating a better profile with Bootstrap
        10. Time for action — checking whether a user is currently logged in
          1. What just happened?
            1. Cleaning up the profile's design
              1. Let's check out our new profile
              2. Adding your changes to Git
        11. Posts
          1. Modeling Posts
            1. How to model posts in MySQL
            2. How to model posts in CouchDB
          2. Have a go hero — setting up the Post class
        12. Creating posts
        13. Time for action — making a function to handle Post creation
          1. What just happened?
        14. Time for action — making a form to enable Post creation
          1. What just happened?
        15. Time for action — creating a route and handling the creation of the Post
          1. What just happened?
          2. Test it out
          3. Adding your changes to Git
        16. Wrapping up
        17. Summary
      15. 8. Using Design Documents for Views and Validation
        1. Design documents
          1. A basic design document
          2. Views
            1. Map functions
        2. Time for action — creating a temporary view
          1. What just happened?
        3. Time for action — creating a view for listing posts
          1. What just happened?
            1. Querying map functions
        4. Time for action — querying the posts_by_user view
          1. What just happened?
            1. Using the view in our application
        5. Time for action — adding support to get_posts_by_user in the post class
          1. What just happened?
        6. Time for action — adding posts to the user profile
          1. What just happened?
            1. Reduce functions
        7. Time for action — creating the reduce function in Futon
          1. What just happened?
        8. Time for action — adding support to our application to consume the reduce function
          1. What just happened?
            1. More with MapReduce
          2. Validation
        9. Time for action — adding support for $_rev to our classes
          1. What just happened?
        10. Time for action — adding support to delete posts in our application
          1. What just happened?
            1. CouchDB's support for validation
        11. Time for action — adding a validate function to ensure that only creators can update or delete their documents
          1. What just happened?
        12. Time for action – hiding the delete buttons when not on the current user's profile
          1. What just happened?
        13. Wrapping up
          1. Want more examples?
          2. Working with design documents in Futon is too hard!
        14. Summary
      16. 9. Adding Bells and Whistles to your Application
        1. Adding jQuery to our project
          1. Installing jQuery
        2. Time for action — adding jQuery to our project
          1. What just happened?
        3. Time for action — creating master.js and connecting Boostrap's JavaScript files
          1. What just happened?
        4. Using jQuery to improve our site
          1. Fixing our delete post action to actually use HTTP delete
        5. Time for action — improving our user experience by using AJAX to delete posts
          1. What just happened?
            1. Updating our route to use the DELETE HTTP method
              1. Let's test it out!
        6. Adding simple pagination using jQuery
        7. Time for action — taking posts out of profile.php and putting them in their own partial view
          1. What just happened?
          2. Adding backend support for pagination
        8. Time for action — adjusting our get_posts_by_user function to skip and limit posts
          1. What just happened?
            1. Let's test it out!
        9. Time for action — refactoring our code so it's not redundant
          1. What just happened?
        10. Time for action — adding frontend support for pagination
          1. What just happened?
        11. Time for action — fixing our delete post function to work with pagination
          1. What just happened?
            1. Testing our complete pagination system
        12. Using Gravatars
        13. Time for action — adding Gravatars to our application
          1. What just happened?
          2. Testing our Gravatars
          3. Adding everything to Git
        14. Summary
      17. 10. Deploying your Application
        1. Before we get started
          1. Application hosting
          2. CouchDB hosting
        2. Database hosting with Cloudant
          1. Getting started with Cloudant
          2. Creating a _users database
          3. Creating a verge database
          4. Have a go hero — give it a shot yourself
          5. Using Futon on Cloudant
          6. Configuring permissions
        3. Configuring our project
        4. Time for action — creating a configuration class
          1. What just happened?
        5. Time for action — adding our configuration file to Bones
          1. What just happened?
          2. Adding changes to Git
        6. Application hosting with PHP Fog
          1. Setting up a PHP Fog account
          2. Creating environment variables
          3. Deploying to PHP Fog
            1. Adding our SSH key to PHP Fog
            2. Connecting to PHP Fog's Git repository
              1. Get the repository from Php Fog
              2. Connecting to the repository from Git
            3. Deploy to PHP Fog
        7. Replicating local data to production
        8. Time for action — replicating our local _users database to Cloudant
          1. What just happened?
          2. Have a go hero — replicating the local verge database to Cloudant
        9. What's next?
          1. Scaling your application
          2. Next steps
        10. Summary
      18. A. Pop quiz — Answers
        1. Chapter 2, Setting up your Development Environment
        2. Chapter 3, Getting Started with CouchDB and Futon
      19. Index