Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Zendcasts: Building a JSON Endpoint Part 1
require '../Slim/Slim.php';
require '../Name.php';
function json($obj)
header('Content-Type', 'application/json');
return json_encode($obj);
$app = new Slim();
$app->config(array('templates.path' => '../templates'));
$app->get('/names/:id',function($id) {
echo json(Name::find($id));
$app->get('/names',function() {
echo json(Name::findAll());
$app->post('/names',function() use ($app) {
$n = new Name(null,$app->request()->post('name'));
$app->get('/', function() use($app) {
if (!isset($_SESSION['names']))
$_SESSION['names'] = array("jane","jim","john","emily","bill","sara");
class Name
public $id;
public $name;
public function __construct($id, $name)
$this->id = $id;
$this->name = $name;
public function create()
$_SESSION['names'][] = $this->name;
public static function findAll()
$names = array();
foreach($_SESSION['names'] as $id => $name)
$names[] = new Name($id , $name);
return $names;
public static function find($id)
return new Name($id, $_SESSION['names'][$id]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.