/** * @param $slim \Slim\Slim */ public function loadRoutes($slim) { parent::loadRoutes($slim); /** * @var $cachedRoutes CachedRoute[] */ $cacheKey = get_called_class() . '.' . get_class() . '::loadRoutes'; if (($systemCache = $this->getApp()->getCacheRepository('system')) != NULL && is_array($cachedRoutes = $systemCache->get($cacheKey))) { // Get routes from cache foreach ($cachedRoutes as $cachedRoute) { $this->mapRestRoute($slim, $cachedRoute->path, $cachedRoute->method, $cachedRoute->httpMethod); } return; } else { $cachedRoutes = array(); } /** * Automatically generates routes for the following * * GET /<model> get all models by id * POST /<model> creates a new model * PUT /<model> bulk creates full new model collection * DELETE /<model> deletes all models * GET /<model>/<id> gets one model's full details * PUT /<model>/<id> updates the model * DELETE /<model>/<id> deletes the model */ // todo: add API versioning $this->addStandardRestRoute($slim, \Slim\Http\Request::METHOD_GET, $cachedRoutes); $this->addStandardRestRoute($slim, \Slim\Http\Request::METHOD_PUT, $cachedRoutes); $this->addStandardRestRoute($slim, \Slim\Http\Request::METHOD_DELETE, $cachedRoutes); /** * Gets other automatically generated routes following the pattern: * /BASE/:id/ACTION(/:param+) from methods named rest<METHOD><ACTION>() * where <METHOD> is GET, PUT, POST, or DELETE */ $rClass = new \ReflectionClass($this); $rMethods = $rClass->getMethods(\ReflectionMethod::IS_PUBLIC); foreach ($rMethods as $rMethod) { // If there is a '@endpoint ignore' property, the function is not served as an endpoint if (in_array('ignore', ReflectionHelper::getDocDirective($rMethod->getDocComment(), 'endpoint'))) { continue; } $action = NULL; $httpMethod = NULL; $methodName = $rMethod->name; if (strpos($methodName, 'restGet', 0) === 0) { $action = strtolower(substr($methodName, 7)); $httpMethod = \Slim\Http\Request::METHOD_GET; } elseif (strpos($methodName, 'restPut', 0) === 0) { $action = strtolower(substr($methodName, 7)); $httpMethod = \Slim\Http\Request::METHOD_PUT; } elseif (strpos($methodName, 'restPost', 0) === 0) { $action = strtolower(substr($methodName, 8)); $httpMethod = \Slim\Http\Request::METHOD_POST; } elseif (strpos($methodName, 'restDelete', 0) === 0) { $action = strtolower(substr($methodName, 10)); $httpMethod = \Slim\Http\Request::METHOD_DELETE; } if (!empty($action)) { $this->mapRestRoute($slim, "/{$this->base}/:id/{$action}(/?)", $methodName, $httpMethod, $cachedRoutes); $this->mapRestRoute($slim, "/{$this->base}/:id/{$action}(/:param+)(/?)", $methodName, $httpMethod, $cachedRoutes); } } if ($systemCache != NULL) { $systemCache->forever($cacheKey, $cachedRoutes); } }