A quick example
So what does a Tonic application look like?
A typical Web app pattern is the front controller pattern, we route all Apache requests through to a single PHP script for processing. The Tonic library then handles the incoming request, loads the resource requested, and generates a response, something like this:
<?php
/*
include Tonic files, we'll presume that they are in the PHP include path
because that makes things a little easier
*/
require_once 'tonic'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'request.php';
require_once 'tonic'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'resource.php';
require_once 'tonic'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'smartyresource.php';
require_once 'tonic'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'response.php';
require_once 'tonic'.DIRECTORY_SEPARATOR.'adapters'.DIRECTORY_SEPARATOR.'fileadapter.php';
/*
we'll create a map of path extensions to mimetypes so that Tonic can
automagically send the correct mimetype header without us having to specify it
for each resource
*/
$mimetypes = array(
'html' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript'
);
/*
create the default persistence adapter to grab our resources from, here we'll
use the file system and point it to the directory named "resources"
*/
$adapter =& new FileAdapter($mimetypes, 'resources');
// create a request object based upon the incoming HTTP request
$request =& new Request();
/*
load the resource mentioned in the request via the request URL and accept headers
*/
$resource =& $request->load($adapter);
/*
execute the resource within the context of the request, this has the effect of
calling the resources get/post/put/delete() method and returning a response object
*/
$response =& $request->exec($adapter, $resource);
/*
if our resource does not have a visible representation (its representation is purely
data), then we can load another resource as its representation format
*/
if ($resource && $representation =& $resource->loadRepresentation($adapter)) {
$response =& $representation->get($request);
}
// output the response doing encoding as required
$response->output();
?>
dispatch.php
So what does a resource look like? A resource is an abstract concept, in Tonic we represent this as a PHP object. The object has methods for each HTTP method we want to respond to and properties containing the data associated with the resource.
<?php
class MyResource extends SmartyResource {
var $myMessage = 'Hello world!';
// respond to HTTP GET method
function &get(&$request) {
$this->_smarty->assign('message', $this->myMessage);
return parent::get($request);
}
}
?>
lib/myresource.php
The data is loaded from a data source, in this case from the file system.
class: MyResource
title: A Quick Example
<html>
<head>
<title>{$this->title|escape}</title>
</head>
<body>
<h1>{$this->myMessage|escape}</h1>
</body>
</html>
resources/helloworld.html
The file system adapter uses the Tonic resource format to hold the resource information within a file. This format is similar to a HTTP response in that it starts with colon separated metadata fields, followed by an emtpy line and then the resource body.
So now requests to /helloworld.html will be routed to our
representation which in turn will load our resource and generate our response.