Skip to content

Instantly share code, notes, and snippets.

@anandkunal
Created December 6, 2010 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anandkunal/729843 to your computer and use it in GitHub Desktop.
Save anandkunal/729843 to your computer and use it in GitHub Desktop.
<?php
require_once 'toro.php';
$blog_posts = array(
array('title' => 'Evening Plans', 'body' => "I'm staying in to work on my law blog."),
array('title' => "You don't need double talk; you need Bob Loblaw", 'body' => 'True.'),
array('title' => "Why should you go to jail for a crime someone else noticed?", 'body' => "You shouldn't."),
);
function display_snippet($pos) {
echo '<a href="article/"' . $blog_post[$pos]['id'] . '</a>' . $blog_post[$pos]['title'] . '</a><br/>';
}
function display_blog_post($pos) {
echo '<h1>' . $blog_post[$pos]['title'] . '</h1><p>' . $blog_post[$pos]['body'] . '</p>';
}
function is_valid_post($pos) {
$pos = intval($pos);
return ($pos >= 0 && $pos < count($blog_posts));
}
class BlogHandler extends ToroHandler {
public function get() {
// Splash page, display all entries.
for ($i=0; $i<count($blog_posts); $i++) {
display_snippet($i);
}
}
public function get_mobile() {
// Only display the last entry for mobile (Loblaw cares).
display_snippet(0);
}
}
class ArticleHandler extends ToroHandler {
public function get($post_id) {
// Display the post if it exists
if (is_valid_post($post_id)) {
display_blog_post($post);
}
else {
header('HTTP/1.0 404 Not Found');
echo 'Post does not exist';
exit;
}
}
}
class CommentHandler extends ToroHandler {
public function post_xhr($post_id) {
// Bob Loblaw never got around to implementing this, here is pseudo PHP
if (is_valid_post($post_id)) {
// Assuming you save this comment, just return JSON if the post exists
echo json_encode(array(
'commentor' => $_POST['commentor'],
'comment_body' => $_POST['comment_body']
));
}
}
}
$site = new ToroApplication(array(
array('/', 'BlogHandler'),
array('article/([a-zA-Z0-9_]+)', 'ArticleHandler'),
array('comment/([a-zA-Z0-9_]+)', 'CommentHandler'),
));
$site->serve();
@justinpfister
Copy link

lets get a public project going on .. MAtt and I have been doing a lot with Php-OO and Silex on Ngin-x

@anandkunal
Copy link
Author

Hey Justin - I already have a project: http://toroweb.org/ - between this and all the other stuff I'm up to, I don't know how much time I'll be able to commit. I'm happy to contribute code and ideas - just don't think I'd make a good full time maintainer :)

@justinpfister
Copy link

justinpfister commented Oct 12, 2011 via email

@justinpfister
Copy link

hey - I forked you. http://toro.kyotist.com/article/Evening%20Plans -- what URL structure are you going for?

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