Add API Sample. Demonstration API using a route table.

This commit is contained in:
PJ Dietz 2012-12-05 10:16:05 -05:00
parent c51577a43a
commit e910418ab1
5 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,8 @@
RewriteEngine on
RewriteBase /wellrested/samples/apisample/
# Send all requests to non-regular files and directories to router.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.+$ index.php [L,QSA]

View File

@ -0,0 +1,40 @@
<?php
require_once(dirname(__FILE__) . '/../../Router.inc.php');
class ApiSampleRouter extends \wellrested\Router {
public function __construct() {
parent::__construct();
$this->addTemplate('/articles/',
'\handlers\ArticleCollectionHandler',
'ArticleCollectionHandler.inc.php');
$this->addTemplate('/articles/{id}',
'\handlers\ArticleItemHandler',
'ArticleItemHandler.inc.php',
array('id' => \wellrested\Route::RE_NUM));
$this->addTemplate('/articles/{slug}',
'\handlers\ArticleItemHandler',
'ArticleItemHandler.inc.php',
array('slug' => \wellrested\Route::RE_SLUG));
}
public function addTemplate($template, $handlerClassName, $handlerFilePath, $variables=null) {
// Customize as needed based on your server.
$template = '/wellrested/samples/apisample' . $template;
$handlerFilePath = dirname(__FILE__) . '/handlers/' . $handlerFilePath;
$this->addRoute(\wellrested\Route::newFromUriTemplate(
$template, $handlerClassName, $handlerFilePath, $variables));
}
}
?>

View File

@ -0,0 +1,19 @@
<?php
namespace handlers;
require_once(dirname(__FILE__) . '/../../../Handler.inc.php');
class ArticleCollectionHandler extends \wellrested\Handler {
protected function get() {
$this->response->statusCode = 200;
$this->response->setHeader('Content-type', 'text/plain');
$this->response->body = 'A list of articles.';
}
}
?>

View File

@ -0,0 +1,20 @@
<?php
namespace handlers;
require_once(dirname(__FILE__) . '/../../../Handler.inc.php');
class ArticleItemHandler extends \wellrested\Handler {
protected function get() {
$this->response->statusCode = 200;
$this->response->setHeader('Content-type', 'text/plain');
$this->response->body = 'One article';
$this->response->body = print_r($this->args, true);
}
}
?>

View File

@ -0,0 +1,10 @@
<?php
require_once('ApiSampleRouter.inc.php');
$router = new ApiSampleRouter();
$response = $router->getResponse();
$response->respond();
exit;
?>