function run() { Todo::setupDb($this->db); $this->app->match('/todos', function () { return $this->dispatch(TodoResource::collection($this->db)); }); $this->app->match('/todos/{id}', function ($id) { return $this->dispatch(TodoResource::entity($this->db, intval($id))); }); $this->app->run(); }
static function entity(\PDO $db, $id) { return Resource::create(self::defaults())->allowedMethods(['GET', 'PUT', 'DELETE'])->isProcessable(self::validator())->canPutToMissing(false)->isNew(false)->isRespondWithEntity(function (Context $context) { return $context->getRequest()->isMethod('PUT'); })->exists(function ($context) use($db, $id) { return Todo::exists($db, $id); })->put(function ($context) use($db, $id) { Todo::update($db, $id, $context->entity); })->delete(function ($context) use($db, $id) { Todo::delete($db, $id); })->handleOk(function (Context $context) use($db, $id) { return Todo::fetchOne($db, $id); }); }