<?php include __DIR__ . '/../../src/Phix/App.php'; \Phix\App::instance()->configure(function ($app) { // Database $pdo = new PDO('sqlite:blog.db'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $app->reg('pdo', $pdo); // Misc $app->reg('site_title', 'My Blog'); })->layout(function ($app, array $vars, $format) { extract($vars); ob_start(); ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title><?php echo $app->reg('site_title'); ?> </title> <link rel="stylesheet" type="text/css" href="<?php echo $app->url(array('css', 'blog.css')); ?> " media="all"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script> <script src="<?php echo $app->url(array('js', 'jquery.relatizeDate.js')); ?> " type="text/javascript"></script>
<?php include __DIR__ . '/../../src/Phix/App.php'; \Phix\App::instance()->reg('data_dir', __DIR__ . '/data')->defaultFormat('json')->format('html', null)->format('xml', null)->format('json', function ($app) { $curr = $app->format('json'); $curr['error'] = function ($app, $status, $msg) { if (is_string($msg)) { $msg = array('error' => 'internal', 'reason' => $msg); } return json_encode($msg); }; return $curr; })->view('response', function ($app, array $vars, $format) { $response = json_encode($vars); // JSONP support if (!empty($_GET['callback']) && preg_match('/^[a-zA-Z_$][0-9a-zA-Z_$]*$/', $_GET['callback'])) { $response = $_GET['callback'] . '(' . $response . ')'; } return $response; })->requestHeader('Content-type', 'application/json')->hook('flush', function ($app) { $app->header('Content-Length: ' . strlen($app->output())); })->get('/:id', function ($app) { $id = $app->param('id'); $file = $app->reg('data_dir') . '/' . $id; if (!file_exists($file)) { $app->error(404, array('error' => 'not_found', 'reason' => 'missing')); } else { $app->status(200); $app->render('response', json_decode(file_get_contents($file))); } })->put('/:id', function ($app) {