Add API Sample. Demonstration API using a route table.
This commit is contained in:
parent
c51577a43a
commit
e910418ab1
|
|
@ -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]
|
||||||
|
|
||||||
|
|
@ -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));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
@ -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.';
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('ApiSampleRouter.inc.php');
|
||||||
|
|
||||||
|
$router = new ApiSampleRouter();
|
||||||
|
$response = $router->getResponse();
|
||||||
|
$response->respond();
|
||||||
|
exit;
|
||||||
|
|
||||||
|
?>
|
||||||
Loading…
Reference in New Issue