My favorites | Sign in
Project Logo
                
Search
for
Updated Jul 08, 2008 by timdashryan
Labels: Featured
GettingStarted  
How to set up an application using the Chowdah library.

Getting Started

Before learning how to build resources, we have to set up the application. Chowdah works in place of your web server to locate a file and display it, so some setup is required to hand control over to Chowdah and load your resources.

The simplest way to do this is to rewrite all requests to a single script, which initializes Chowdah and loads "root resource" of your application. Note that the folder structure used in this tutorial is only for example; you can modify it to suit your needs.

Setting up Folders

In the root of your application, create a folder to hold your scripts (preferably with a leading period, such as '.application'). Download the latest Chowdah package, and copy the 'chowdah' folder into the newly created scripts folder. Also create a folder to hold your resources. Your folder structure should now resemble:

/
/.application/
/.application/chowdah/ (containing the chowdah library)
/.application/resources/

Request Handler

Next, we need a script to handle HTTP requests and forward them to the Chowdah library. In your application directory, create a script named request.php with the following code:

<?php

// load Chowdah class
require 'chowdah/Chowdah.php';

// load application classes
import('resources');

// load Chowdah
Chowdah::init();
Chowdah::handleCurrentRequest(new RootResource())->send();

?>

This script first loads the Chowdah class, then any classes specific to your application, including your resources. It then initializes Chowdah. Finally, it calls the Chowdah class to handle the current HTTP request, starting with the resource RootResource, and sends the response to the browser.

Redirecting

We must now redirect all HTTP requests to this script. On Apache servers, this is easy: In the root of your application, create an .htaccess file with the following:

# rewrite all requests to the handler
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^.* .application/request.php [E=REMOTE_USER:%{HTTP:Authorization}]
</IfModule>
	
# the following features are disabled to prevent them from modifying
# URLs (Chowdah takes care of any potential security issues)
<IfModule mod_security.c>
  SecFilterEngine Off
</IfModule>
<IfModule mod_dir.c>
  DirectorySlash Off
</IfModule>

On IIS or other servers, install a URL rewriting package and follow its instructions to forward requests to request.php.

Your First Resource

In our above script, we called handleCurrentRequest with a new RootResource. Let's create this resource now.

In the resources directory, create a script called RootResource.php. (The import function we called earlier will automatically load this class). Then save it with the following script:

<?php 

class RootResource extends HTTPResourceBase
{
	protected $methods = array('GET', 'OPTIONS');
	
	public function GET(HTTPRequest $request)
	{
		// create a simple webpage
		$response = new HTTPResponse();
		$response->setContent('Hello World!');
		$response->setContentType(new MIMEType('text', 'plain'));
		return $response;
	}
}

?>

Now try loading your application in a web browser. Congratulations! You've just made your first Chowdah application.

Next: WorkingWithResources


Sign in to add a comment
Hosted by Google Code