/** * @param string $method * @param string $route * @param object $routerClass * @param string $callback * @param Secured $secured * @throws \Exception */ private function setRoute($method, $route, $routerClass, $callback, $secured) { $method = strtoupper($method); $easyRoute = new Route($route, $routerClass, $callback, $secured, $this); if ($method === 'GET') { $this->app->get($route, array($easyRoute, 'call')); } elseif ($method === 'PUT') { $this->app->put($route, array($easyRoute, 'call')); } elseif ($method === 'POST') { $this->app->post($route, array($easyRoute, 'call')); } elseif ($method === 'DELETE') { $this->app->delete($route, array($easyRoute, 'call')); } else { throw new \Exception('Unsupported HTTP method ' . $method); } }
$container['auth'] = function ($container) { return new BB8\Emoji\Auth($container); }; //Initialize the slim app $app = new App($container); //Add middleware at app level $app->add('BB8\\Emoji\\Middleware:init'); //Index page $app->get('/', 'BB8\\Emoji\\Controllers\\UserController:index'); //Create new user $app->post('/signup', 'BB8\\Emoji\\Controllers\\UserController:create'); //Login Route $app->post('/auth/login', 'BB8\\Emoji\\Controllers\\UserController:login'); //Logout Route $app->get('/auth/logout', 'BB8\\Emoji\\Controllers\\UserController:logout')->add('BB8\\Emoji\\Middleware:authorize'); //List all emojis Route $app->get('/emojis', 'BB8\\Emoji\\Controllers\\EmojiController:index'); //Gets an emoji $app->get('/emojis/{id}', 'BB8\\Emoji\\Controllers\\EmojiController:show'); //Adds a new Emoji $app->post('/emojis', 'BB8\\Emoji\\Controllers\\EmojiController:create')->add('BB8\\Emoji\\Middleware:authorize'); //Updates an Emoji $app->put('/emojis/{id}', 'BB8\\Emoji\\Controllers\\EmojiController:update')->add('BB8\\Emoji\\Middleware:authorize'); //Updates an Emoji Keyword $app->put('/emojis/{id}/{kId}', 'BB8\\Emoji\\Controllers\\EmojiController:updateKey')->add('BB8\\Emoji\\Middleware:authorize'); //Partially Updates an Emoji $app->patch('/emojis/{id}', 'BB8\\Emoji\\Controllers\\EmojiController:update')->add('BB8\\Emoji\\Middleware:authorize'); //Deletes an Emoji $app->delete('/emojis/{id}', 'BB8\\Emoji\\Controllers\\EmojiController:destroy')->add('BB8\\Emoji\\Middleware:authorize'); //Load and run the application $app->run();
public function testDeleteRoute() { $path = '/foo'; $callable = function ($req, $res) { // Do something }; $app = new App(); $route = $app->delete($path, $callable); $this->assertInstanceOf('\\Slim\\Route', $route); $this->assertAttributeContains('DELETE', 'methods', $route); }
return ResultWrapper::getResult(Comment::getByPromo($promoId, $page), $response); } catch (Exception $e) { return ResultWrapper::getError($e->getMessage(), $response); } }); $app->get("/comment-user/{userId}/{page}", function (Request $request, Response $response, $userId, $page) { try { return ResultWrapper::getResult(Comment::getByUser($userId, $page), $response); } catch (Exception $e) { return ResultWrapper::getError($e->getMessage(), $response); } }); $app->delete('/comment/{id}', function (Request $request, Response $response, $id) { try { $token = $request->getHeader('token'); return ResultWrapper::getResult(Comment::del($token, $id), $response); } catch (Exception $e) { return ResultWrapper::getError($e->getMessage(), $response); } }); //For scraping data from the website only $app->get('/scrap-data/{category}/{page}', function (Request $request, Response $response, $category, $page) { try { return ResultWrapper::getResult(Scraper::scrapListPromo($category, $page), $response); } catch (Exception $e) { return ResultWrapper::getError($e->getMessage(), $response); } }); $app->get('/fill-empty-desc', function (Request $request, Response $response) { try { return ResultWrapper::getResult(Scraper::fillDescriptionPromo(), $response); } catch (Exception $e) {
/** * Add a repository to your Slim App * @param App $app * @param Repository $repository * @return App the given app for chaining */ public function bootStrap(App $app, Repository $repository) { $baseUrl = '/' . self::parseRepoName($repository->getEntityClass()->getShortName()); /** * Get the whole collection. */ $app->get($baseUrl, function (Request $request, Response $response) use($repository) { return $response->write(self::output($repository->findAll()))->withHeader('Content-Type', 'application/json'); }); /** * Delete the whole collection. */ $app->delete($baseUrl, function (Request $request, Response $response) use($repository) { $repository->deleteAll(); }); /** * Add a new entity to the collection. */ $app->post($baseUrl, function (Request $request, Response $response) use($repository) { $body = self::getBody($request->getBody(), $repository->getEntityClass(), $response); if ($body instanceof Response) { return $body; } else { // Store the entity $repository->insert($body); return $response->withStatus(Status::CREATED)->withHeader('Content-Type', 'application/json')->write(self::output($body)); } }); if ($this->showCheckPage) { /** * Display the repository check page. */ $app->get($baseUrl . '/check', function (Request $request, Response $response) use($repository) { $repository->checkDatabase(); }); } $entityUrl = $baseUrl . '/{id}'; /** * Get a single entity. */ $app->get($entityUrl, function (Request $request, Response $response, $args) use($repository) { $entity = $repository->get($args['id']); if ($entity) { return $response->write(self::output($entity))->withHeader('Content-Type', 'application/json'); } return $response->withStatus(Status::NOT_FOUND); }); /** * Delete a single entity */ $app->delete($entityUrl, function (Request $request, Response $response, $args) use($repository) { $repository->delete($args['id']); }); /** * Replace a single entity */ $app->put($entityUrl, function (Request $request, Response $response, $args) use($repository) { $body = self::getBody($request->getBody(), $repository->getEntityClass(), $response); if ($body instanceof Response) { return $body; } else { // Store the entity $repository->getIdProperty()->setValue($body, $args['id']); $repository->update($body); return $response->withHeader('Content-Type', 'application/json')->write(self::output($body)); } }); return $app; }