Namespace API sample.

Add and update comments.
This commit is contained in:
PJ Dietz 2012-12-05 16:08:30 -05:00
parent ff331935d2
commit a2255bebd9
5 changed files with 44 additions and 9 deletions

View File

@ -1,7 +1,12 @@
<?php
namespace apisample;
require_once(dirname(__FILE__) . '/../../Router.inc.php');
/**
* Loads and instantiates handlers based on URI.
*/
class ApiSampleRouter extends \wellrested\Router {
public function __construct() {
@ -9,16 +14,16 @@ class ApiSampleRouter extends \wellrested\Router {
parent::__construct();
$this->addTemplate('/articles/',
'\handlers\ArticleCollectionHandler',
'ArticleCollectionHandler',
'ArticleCollectionHandler.inc.php');
$this->addTemplate('/articles/{id}',
'\handlers\ArticleItemHandler',
'ArticleItemHandler',
'ArticleItemHandler.inc.php',
array('id' => \wellrested\Route::RE_NUM));
$this->addTemplate('/articles/{slug}',
'\handlers\ArticleItemHandler',
'ArticleItemHandler',
'ArticleItemHandler.inc.php',
array('slug' => \wellrested\Route::RE_SLUG));
@ -28,6 +33,7 @@ class ApiSampleRouter extends \wellrested\Router {
// Customize as needed based on your server.
$template = '/wellrested/samples/apisample' . $template;
$handlerClassName = '\apisample\handlers\\' . $handlerClassName;
$handlerFilePath = dirname(__FILE__) . '/handlers/' . $handlerFilePath;
$this->addRoute(\wellrested\Route::newFromUriTemplate(

View File

@ -1,5 +1,10 @@
<?php
namespace apisample\data;
/**
* Simple class for reading and writing articles to a text file.
*/
class ArticlesControler {
public $data;

View File

@ -1,16 +1,22 @@
<?php
namespace handlers;
namespace apisample\handlers;
require_once(dirname(__FILE__) . '/../../../Handler.inc.php');
require_once(dirname(__FILE__) . '/../data/ArticlesControler.inc.php');
/**
* Handler class for a list of articles.
*/
class ArticleCollectionHandler extends \wellrested\Handler {
/**
* Respond to a GET request.
*/
protected function get() {
// Display the list of articles.
$articles = new \ArticlesControler();
$articles = new \apisample\data\ArticlesControler();
if (isset($articles->data)) {
@ -28,6 +34,9 @@ class ArticleCollectionHandler extends \wellrested\Handler {
}
/**
* Respond to a POST request.
*/
protected function post() {
// Read the request body, and ensure it is in the proper format.
@ -64,7 +73,7 @@ class ArticleCollectionHandler extends \wellrested\Handler {
}
// Ensure slug is not a duplicate.
$articles = new \ArticlesControler();
$articles = new \apisample\data\ArticlesControler();
if ($articles->getArticleBySlug($article['slug']) !== false) {
$this->response->statusCode = 409;
$this->response->setHeader('Content-type', 'text/plain');

View File

@ -1,16 +1,25 @@
<?php
namespace handlers;
namespace apisample\handlers;
require_once(dirname(__FILE__) . '/../../../Handler.inc.php');
require_once(dirname(__FILE__) . '/../data/ArticlesControler.inc.php');
/**
* Handler class for one specific article.
*
* When instantiated by the Router, this class should receive an id or slug
* argument to identify the article.
*/
class ArticleItemHandler extends \wellrested\Handler {
/**
* Respond to a GET request.
*/
protected function get() {
// Read the list of articles.
$articles = new \ArticlesControler();
$articles = new \apisample\data\ArticlesControler();
$article = false;
@ -41,6 +50,9 @@ class ArticleItemHandler extends \wellrested\Handler {
}
/**
* Respond to a PUT request.
*/
protected function put() {
// Read the request body, and ensure it is in the proper format.
@ -141,6 +153,9 @@ class ArticleItemHandler extends \wellrested\Handler {
}
/**
* Respond to a DELETE request.
*/
protected function delete() {
// Read the list of articles.

View File

@ -2,7 +2,7 @@
require_once('ApiSampleRouter.inc.php');
$router = new ApiSampleRouter();
$router = new \apisample\ApiSampleRouter();
$response = $router->getResponse();
$response->respond();
exit;