Example #1
0
 /**
  * @return array
  * @throws \Exception
  */
 public function getCollections()
 {
     /** @var Config $collectionConfig */
     $collectionConfig = $this->getDI()->get('collectionConfig');
     $collections = [];
     if (!$collectionConfig) {
         return [];
     }
     foreach ($collectionConfig->getRequiredArray('versions') as $version => $entitys) {
         foreach ($entitys as $entityName => $entity) {
             $collection = new PhalconCollection();
             $collection->setPrefix(sprintf('/%s/%s', strtolower($version), strtolower($entityName)));
             $collection->setHandler(sprintf('\\%s\\%s\\Controllers\\%s\\%sController', $collectionConfig->getRequiredString('namespace'), $version, $entityName, $entityName));
             $collection->setLazy(true);
             foreach ($entity as $requestMethod => $actions) {
                 foreach ($actions as $actionName => $action) {
                     $validMethod = in_array(strtoupper($requestMethod), RequestMethodEnum::getConstants());
                     if (!$validMethod) {
                         throw new \Exception("Invalid request method in the config file: '{$requestMethod}'");
                     }
                     $requestMethod = strtolower($requestMethod);
                     $collection->{$requestMethod}($action, $actionName);
                 }
             }
             $collections[] = $collection;
         }
     }
     return $collections;
 }
Example #2
0
$app->get('/', function () use($app) {
    $routes = $app->getRouter()->getRoutes();
    $routeDefinitions = array('GET' => array(), 'POST' => array(), 'PUT' => array(), 'PATCH' => array(), 'DELETE' => array(), 'HEAD' => array(), 'OPTIONS' => array());
    foreach ($routes as $route) {
        $method = $route->getHttpMethods();
        $routeDefinitions[$method][] = $route->getPattern();
    }
    return $routeDefinitions;
});
/**
 * Collections let us define groups of routes that will all use the same controller.
 * We can also set the handler to be lazy loaded.  Collections can share a common prefix.
 * @var $exampleCollection
 */
$exampleCollection = new \Phalcon\Mvc\Micro\Collection();
$exampleCollection->setLazy(true)->setPrefix('/v1/example')->setHandler('\\PhalconRest\\Controllers\\ExampleController');
// Set Access-Control-Allow headers.
$exampleCollection->options('/', 'optionsBase');
$exampleCollection->options('/{id}', 'optionsOne');
// First paramter is the route, which with the collection prefix here would be GET /example/
// Second paramter is the function name of the Controller.
$exampleCollection->get('/', 'get');
// This is exactly the same execution as GET, but the Response has no body.
$exampleCollection->head('/', 'get');
// $id will be passed as a parameter to the Controller's specified function
$exampleCollection->get('/{id:[0-9]+}', 'getOne');
$exampleCollection->head('/{id:[0-9]+}', 'getOne');
$exampleCollection->post('/', 'post');
$exampleCollection->delete('/{id:[0-9]+}', 'delete');
$exampleCollection->put('/{id:[0-9]+}', 'put');
$exampleCollection->patch('/{id:[0-9]+}', 'patch');
Example #3
0
 /**
  * Get collections
  *
  * @return array
  */
 public function getCollections()
 {
     $collectionConfig = $this->getDI()->get('collectionConfig');
     $collections = [];
     if (!$collectionConfig) {
         return [];
     }
     foreach ($collectionConfig as $version => $entitys) {
         foreach ($entitys as $entityName => $entity) {
             $collection = new PhalconCollection();
             $collection->setPrefix(sprintf('/%s/%s', strtolower($version), strtolower($entityName)));
             $collection->setHandler(sprintf('\\%s\\%s\\Controllers\\%s\\%sController', $this->getDI()->get('config')->namespace, $version, $entityName, $entityName));
             $collection->setLazy(true);
             foreach ($entity as $requestMethod => $actions) {
                 foreach ($actions as $actionName => $action) {
                     if (!in_array(strtoupper($requestMethod), (new \ReflectionClass(PhrestRequest::class))->getConstants())) {
                         throw new \Exception("Invalid request method in the config file: '{$requestMethod}'");
                     }
                     $requestMethod = strtolower($requestMethod);
                     $collection->{$requestMethod}($action, $actionName);
                 }
             }
             $collections[] = $collection;
         }
     }
     return $collections;
 }