Ejemplo n.º 1
0
//Set shared Facebook SDK
$di->setShared('facebook', function () {
    return new Facebook\Facebook(['app_id' => '976309079106997', 'app_secret' => '3d08707832a17ab10369f4f0643618aa', 'default_graph_version' => 'v2.4']);
});
//Set request object
$di->set("request", "Phalcon\\Http\\Request", true);
//Instantiate Phalcon Micro framework
$app = new Micro();
$app->setDI($di);
//Create response object
$response = new Response();
/**
 * Get random image
 */
$app->get('/images/random', function () use($app, $response) {
    $manager = $app->getDI()->get('modelsManager');
    $minMax = $manager->createBuilder()->from('Images')->columns('min(id) as minimum, max(id) as maximum')->getQuery()->getSingleResult();
    $id = rand($minMax->minimum, $minMax->maximum);
    $image = new Images();
    $response->setJsonContent($image->getImageInfo($id));
    $response->send();
});
/**
 * Get specific image
 */
$app->get('/image/{id:[0-9]+}', function ($id) use($app, $response) {
    $image = new Images();
    $response->setJsonContent($image->getImageInfo($id));
    $response->send();
});
/**
 /**
  * Add specified headers to a response.
  **/
 protected function addResponseHeaders(Micro $app, Response $response)
 {
     $appConfig = $app->getDI()->get(Application::DI_CONFIG);
     if (isset($appConfig['cors'])) {
         $response->setHeader('Access-Control-Allow-Origin', $appConfig['cors']);
     }
     //Attach headers always to request.
     foreach (static::$headers as $headerKey => $headerValue) {
         $response->setHeader($headerKey, $headerValue);
     }
     return $this;
 }
 /**
  * Add specified headers to a response.
  **/
 protected function addResponseHeaders(Micro $app, Response $response)
 {
     $appConfig = $app->getDI()->get(Application::DI_CONFIG);
     if (isset($appConfig['cors'])) {
         $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
         $origins = explode(';', $appConfig['cors']);
         $origins = array_map('trim', $origins);
         if (in_array($origin, $origins)) {
             $response->setHeader('Access-Control-Allow-Origin', $origin);
         }
     } else {
         $response->setHeader('Access-Control-Allow-Origin', '*');
     }
     //Attach headers always to request.
     foreach (static::$headers as $headerKey => $headerValue) {
         $response->setHeader($headerKey, $headerValue);
     }
     return $this;
 }