Preparations

Before you can start coding the application logic, you need to prepare both the database and the application. To get started, you need to import the beer-sample bucket, prepare some views and make sure everything works correctly. You’ll also get the application skeleton set up so you are ready to code.

Setting up Couchbase Server

If you haven’t already, download and install Couchbase Server 2.0. While you’re at it, make sure to install the beer-sample sample bucket on the fly. If you already have the server installed and the beer-sample bucket is not in place, head over to Settings->Sample Buckets and install it. Give it a few seconds until the notification box disappears. You may need to shrink the size of some of your existing buckets to make room for the beer-sample database.

The beer-sample bucket comes with a small set of views already predefined, but to make our application function correctly we need some more. This is also a very good chance to explore the view management possibilities inside the Web UI.

Since we want to list beers and breweries by name, we need to define one view for each. Head over to the Web UI and click on the Views menu. Select beer-sample from the drop-down list to switch to the correct bucket. Now click on Development Views and then Create Development View to define your first view. You need to give it the name of both the design document and the actual view. Insert the following names:

  • Design Document Name: _design/dev_beer

  • View Name: by_name

The next step is to define the map and (optional) reduce functions. In our examples, we won’t use have a reduce function, but you can play around if you would like to. Insert the following JavaScript map function and click Save.

function (doc, meta) {
  if(doc.type && doc.type == "beer") {
    emit(doc.name, null);
  }
}

Every map function takes the full document ( doc ) and (optionally) its associated metadata ( meta ) as the arguments. You are then free to inspect this data and emit a result when you want to have it in your view. Views are always sorted by key, so by emitting information, we are in effect creating an index. In our case we emit the name of the beer ( doc.name ) when the document both has a type field and the type is beer. We don’t need to emit a value - that’s because we are using null here. It’s always advisable to keep the view entry as small as possible. Resist the urge to include the full document through emit(meta.id, doc), because it will increase the size of your views. If you need to access the full document (or large parts), then retrieve the document via the returned id in the view result. Note: at this time PHP does not have a way to include docs, though some other Couchbase SDKs do.

Now we need to do (nearly) the same for our breweries. Since you already know how to do this, here is all the information you need to create it:

  • Design Document Name: _design/dev_brewery

  • View Name: by_name

  • Map Function:

    function (doc, meta) {
      if(doc.type && doc.type == "brewery") {
        emit(doc.name, null);
      }
    }

The final step that you need to do is to push the design documents in production. While the design documents are in development, the index is only applied to a subset of the data. Since we want to have the index on the whole dataset, click the Publish button on both design documents (and accept any info popup that warns you from overriding the old one). See the view section of the Couchbase Server manual for more information on how you may use this development and production workflow to your advantage when developing a large application.

Installing the Application Dependencies

Now that Couchbase Server is ready to use, we need to set up the skeleton of our application. Since we’re using composer, all we need to get the dependencies is to create a composer.json file with the following content:

{
  "require": {
    "silex/silex": "1.0.x-dev",
    "twig/twig": ">=1.8,<2.0-dev"
  }
}

Place that file inside the /beersample-php directory of your web root (depending on your setup, it is often located under /var/www/ ). We also need to create a few more directories to keep the application organized.

Create directories with the following structure:

/beersample-php
    /templates
        /beers
        /breweries
    /assets
        /css
        /js

We’ll fill the template directories later, but the assets can be added immediately. Please locate and download the following JavaScript and CSS files so they are in place. We make use of the fabulous Twitter Bootstrap library to make the application look good without much effort.

Also, we’re using pretty URLs in our application. Here is a .htaccess file you can place inside your root directory to make it work properly when using Apache HTTPD. Please refer to the Silex documentation on how to add one for different web servers.

<IfModule mod_rewrite.c>
    Options -MultiViews

    RewriteEngine On
    RewriteBase /beersample-php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

To install the dependencies, we’re now able to run php composer.phar install (or update ). It should install all needed dependencies and we’re ready to go afterwards:

Loading composer repositories with package information
Installing dependencies
  - Installing twig/twig (v1.11.1)
    Downloading: 100%
...

  - Installing silex/silex (dev-master 0e69dc2)
    Cloning 0e69dc22400293f9364f8b918d008f3f6b634a47

symfony/routing suggests installing symfony/config (2.1.*)
...
Writing lock file
Generating autoload files