Skip to content

Instantly share code, notes, and snippets.

@alganet
Created August 25, 2012 14:55
Show Gist options
  • Save alganet/3466746 to your computer and use it in GitHub Desktop.
Save alganet/3466746 to your computer and use it in GitHub Desktop.
Full RESTful/Relational Blog in less than 100 lines [Work In Progress]
<?php
use Respect\Rest\Router;
use Respect\Relational\Mapper;
use Respect\Template\Xml;
use Respect\Template\Html;
$db = new Mapper(new PDO));
$db->postsBase = $db->select('id', 'title', 'text')->posts(
$db->select('name', 'bio')->author)
);
$db->publicPosts = $db->postsBase->pendingAll();
$db->singlePost = $db->publicPosts->pending('id');
$db->archiveItem = $db->publicPosts->pendingAll(
'YEAR(created_at)',
'MONTH(created_at)',
'DAY(created_at)'
);
$db->nextPost = $db->post->limit(1)->pending('id>');
$db->prevPost = $db->post->limit(1)->pending('id<');
$db->topPost = $db->post->limit(1)->pending('MAX(id)');
$db->postComments = $db->comment->pending('post_id');
$db->userAuth = $db->users->pending('username', 'MD5(password)');
$app = new Router;
$app->always('Accept', array(
'application/json' => 'json_encode'
));
$htmlPostCollection = Html::schemaOrg('Blog');
$htmlPostItem = Html::schemaOrg('BlogPosting');
$xmlPostCollection = Xml::schemaOrg('Blog');
$xmlPostItem = Xml::schemaOrg('BlogPosting');
$app->post('/posts', $db->publicPosts->waiting('id', 'title', 'text', 'author_id')->authDigest('Sample Blog', $db->userAuth);
$app->post('/posts/*', $db->postComments->waiting('id', 'email', 'text');
$app->get('/posts', $db->publicPosts)
->rel(array(
'item' => $app->get('/posts/*', $db->singlePost)
->rev('id') //id from parent data
->rel(array(
'comment' => $app->get('/posts/*/comments' $db->postComments)
->rev('id')
->accept($postItemView = array(
'text/html' => $htmlPostItem,
'text/xml' => $xmlPostItem))
'top' => $app->get('/posts/top', $db->topPost )->accept($postItemView),
'next' => $app->get(null, $db->nextPost)->accept($postItemView),
'prev' => $app->get(null, $db->prevPost)->accept($postItemView)))
->accept(array(
'text/html' => $htmlPostItem,
'text/xml' => $xmlPostItem))
'archive' => array(
'year' => $archiveItem = $app->get('/posts/archive/*/*/*', $db->archiveItem)
->accept(array(
'text/html' => $htmlPostArchive,
'text/xml' => $xmlPostArchive))
'month' => $archiveItem,
'day' => $archiveItem
))->accept(array(
'text/html' => $htmlPostCollection
'text/xml' => $xmlPostCollection
));
@alganet
Copy link
Author

alganet commented Aug 26, 2012

@hussani not yet. ->pending, waiting(), ->rel, ->rev and ->schemaOrg are missing.

@hussani
Copy link

hussani commented Oct 1, 2012

How would function the schemaOrg function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment