protected function addRoutes(ControllerCollection $controllers) { $controllers->post('/key', [$this, 'create']); $controllers->get('/key/{id}', [$this, 'show']); $controllers->put('/key/{id}', [$this, 'update']); $controllers->delete('/key/{id}', [$this, 'delete']); }
public function connect(Application $app) { $this->app = $app; // api v1 $api = new ControllerCollection(new Route()); $api->get('/{model}', __CLASS__ . '::listAction')->assert('model', '^([a-z]+)$'); $api->get('/{model}/{id}', __CLASS__ . '::itemAction')->assert('model', '^([a-z]+)$')->assert('id', '^([a-z0-9]+)$'); $api->delete('/{model}/{id}', __CLASS__ . '::deleteAction')->assert('model', '^([a-z]+)$')->assert('id', '^([a-z0-9]+)$'); $api->put('/{model}/{id}', __CLASS__ . '::updateAction')->assert('model', '^([a-z]+)$')->assert('id', '^([a-z0-9]+)$'); $api->post('/{model}', __CLASS__ . '::createAction')->assert('model', '^([a-z]+)$'); // $model = $this->checkModel('message'); // // foreach (range(900, 1000) as $id) // { // $message = new $model; // // $message->text = $id; // $message->date = time(); // // $this->dm->persist($message); // } // // $this->dm->flush(); return $api; }
/** * * * @param ControllerCollection $factory * @return null */ public function createRoutes(ControllerCollection $factory) { $factory->get('/files', [$this, 'get']); $factory->get('/files/{hash}', [$this, 'getOne']); $factory->post('/files/{hash}', [$this, 'userAction']); $factory->delete('/files/{hash}', [$this, 'remove'])->onlyAdmin(); $factory->put('/files/{hash}', [$this, 'update'])->onlyAdmin(); $factory->get('/artists', [$this, 'getArtists']); }
protected function addRoutes(ControllerCollection $controllers) { $controllers->post('/api/users', array($this, 'newAction')); $controllers->get('/api/users/{name}', array($this, 'showAction'))->bind('api_users_show'); $controllers->get('/api/users', array($this, 'listAction')); $controllers->put('/api/users/{name}', array($this, 'updateAction')); $controllers->delete('/api/users/{name}', array($this, 'deleteAction')); $controllers->match('api/users/{name}', array($this, 'updateAction'))->method('PATCH'); }
/** * @param Server $app * * @return ControllerCollection */ public function connect(Server $app) { $_controllers = new ControllerCollection(); //************************************************************************* //* GET //************************************************************************* $_controllers->get('/{id}', function (Application $app, $id) { if (!isset($id)) { throw new \InvalidArgumentException('You must specify an "id".'); } // Have to base64 encode id cuz it's funky $_id = base64_decode(urldecode($id)); \Kisma\Utility\Log::trace('get /contact/' . $_id); $_controller = new ContactsController(); if (null !== ($_result = $_controller->findById($_id))) { return $_controller->createResponse($_id, $_result); } return new Response('you suck Not found'); }); //************************************************************************* //* POST //************************************************************************* $_controllers->post('/', function (Application $app) { $_returnUrl = $app['request']->get('return_url'); $_id = $app['request']->get('id'); if (!isset($_id)) { throw new \InvalidArgumentException('You must specify an "id".'); } $_id = base64_decode(urldecode($_id)); \Kisma\Utility\Log::trace('post /contact/' . ($_id ?: '[NEW]') . print_r($_POST, true)); $_controller = new ContactsController(); if (null === ($_result = $_controller->findById($_id))) { // Ajax request? Return a JSON response if ('XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH']) { return $_controller->createResponse($_id, 'Contact not found!', 404); } return 'Contact not found!'; } // Copy to id as well $_POST['id'] = $_id; try { $_result = $_controller->updateContact(new \Teledini\Components\Contacts\GenericContact($_POST)); \Kisma\Utility\Log::trace('post result: ' . print_r($_result, true)); $_SESSION['contact_post_result'] = $_result; } catch (\Exception $_ex) { // Error \Kisma\Utility\Log::error('Exception saving contact: ' . $_ex->getMessage()); } // Ajax request? Return a JSON response if ('XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH']) { return $_controller->createResponse($_id, 'Contact updated.'); } if ($_returnUrl) { // Redirect header('Location: ' . $_returnUrl); die; } }); //************************************************************************* //* DELETE //************************************************************************* $_controllers->delete('/{id}', function (Application $app, $id) { $_returnUrl = $app['request']->get('return_url'); $_id = $app['request']->get('id'); if (!isset($_id)) { throw new \InvalidArgumentException('You must specify an "id".'); } $_id = base64_decode(urldecode($_id)); \Kisma\Utility\Log::trace('post /contact/' . ($_id ?: '[NEW]') . print_r($_POST, true)); $_controller = new ContactsController(); if (null === ($_result = $_controller->findById($_id))) { // Ajax request? Return a JSON response if ('XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH']) { return $_controller->createResponse($_id, 'Contact not found!', 404); } return 'Contact not found!'; } // Copy to id as well $_POST['id'] = $_id; try { $_result = $_controller->updateContact(new \Teledini\Components\Contacts\GenericContact($_POST)); \Kisma\Utility\Log::trace('post result: ' . print_r($_result, true)); $_SESSION['contact_post_result'] = $_result; } catch (\Exception $_ex) { // Error \Kisma\Utility\Log::error('Exception saving contact: ' . $_ex->getMessage()); } // Ajax request? Return a JSON response if ('XMLHttpRequest' == $_SERVER['HTTP_X_REQUESTED_WITH']) { return $_controller->createResponse($_id, 'Contact updated.'); } if ($_returnUrl) { // Redirect header('Location: ' . $_returnUrl); die; } }); return $_controllers; }
/** * Setup the basic crud endpoints for this controller * @param ControllerCollection $controller * @return ControllerCollection */ public function addCrudOperations(ControllerCollection $controller) { $controller->get("/search", array($this, "search")); $controller->get("/{id}", array($this, "get")); $controller->post("/", array($this, "create")); $controller->put("/{id}", array($this, "update")); $controller->delete("/{id}", array($this, "delete")); return $controller; }
/** * {@inheritdoc} */ public function connect(Application $app) { $modelName = $this->modelName; $prefix = sprintf('rest_controller.%s.', $this->modelName); if (null !== $this->modelClass) { $app[$prefix . 'model_class'] = $this->modelClass; } if (null !== $this->lastModifiedGetter) { $app[$prefix . 'last_modified_getter'] = $this->lastModifiedGetter; } if (isset($app[$prefix . 'model_class'])) { $app[$prefix . 'query_class'] = $app[$prefix . 'model_class'] . 'Query'; } else { throw new \InvalidArgumentException(sprintf('You have to configure the "%s.model_class" parameter.', $prefix)); } $controllers = new ControllerCollection($app['route_factory']); /** * Returns all objects */ $controllers->get('/', function () use($app, $prefix) { $query = new $app[$prefix . 'query_class'](); return new Response($query->find()->exportTo($app['json_parser']), 200, array('Content-Type' => 'application/json')); }); /** * Returns a specific object identified by a given id */ $controllers->get('/{id}', function ($id) use($app, $prefix, $modelName) { $query = new $app[$prefix . 'query_class'](); $object = $query->findPk($id); if (!$object instanceof $app[$prefix . 'model_class']) { throw new NotFoundHttpException(sprintf('%s with id "%d" does not exist.', ucfirst($modelName), $id)); } $response = new Response($object->exportTo($app['json_parser']), 200, array('Content-Type' => 'application/json')); if (isset($app[$prefix . 'last_modified_getter'])) { $response->setLastModified($object->{$app}[$prefix . 'last_modified_getter']()); } return $response; }); /** * Create a new object */ $controllers->post('/', function (Request $request) use($app, $prefix) { $object = new $app[$prefix . 'model_class'](); $object->fromArray($request->request->all()); $object->save(); return new Response($object->exportTo($app['json_parser']), 201, array('Content-Type' => 'application/json')); }); /** * Update a object identified by a given id */ $controllers->put('/{id}', function ($id, Request $request) use($app, $prefix, $modelName) { $query = new $app[$prefix . 'query_class'](); $object = $query->findPk($id); if (!$object instanceof $app[$prefix . 'model_class']) { throw new NotFoundHttpException(sprintf('%s with id "%d" does not exist.', ucfirst($modelName), $id)); } $object->fromArray($request->request->all()); $object->save(); if (isset($app['monolog'])) { $app['monolog']->addInfo(sprintf('Update %s with id %d', ucfirst($modelName), $id)); } return new Response($object->exportTo($app['json_parser']), 200, array('Content-Type' => 'application/json')); }); /** * Delete a object identified by a given id */ $controllers->delete('/{id}', function ($id) use($app, $prefix) { $query = new $app[$prefix . 'query_class'](); $query->filterByPrimaryKey($id)->delete(); return new Response('', 204, array('Content-Type' => 'application/json')); }); return $controllers; }