/**
  * @param \Phalcon\Mvc\Micro
  **/
 public function mutateResponse(Micro $app, Response $response = null, $content = null)
 {
     //If we didn't pass optional parameters, use the apps.
     if (is_null($content)) {
         $content = $app->getReturnedValue();
     }
     if (!$response instanceof Response) {
         $response = $app->response;
     }
     $this->addResponseHeaders($app, $response);
     if ($content instanceof Response) {
         //We play safe here, fearing of API changes.
         if (method_exists($content, 'getHeaders')) {
             $headers = $content->getHeaders();
             if (method_exists($headers, 'toArray')) {
                 $headersArr = $headers->toArray();
                 if (is_array($headersArr)) {
                     foreach ($headersArr as $headerKey => $headerValue) {
                         $response->setHeader($headerKey, $headerValue);
                     }
                 }
             }
         }
         $responseValue = $content->getContent();
     } else {
         $responseValue = $content;
     }
     if (method_exists($this, $this->getResponseFormat())) {
         $this->{$this->getResponseFormat()}($responseValue, $app);
     }
     //Dispatch response if the controller response has not been sent...
     if (!$content instanceof Response || !$content->isSent()) {
         //...and we have not dispatched the app response.
         if (!$response->isSent()) {
             $response->send();
         }
     }
 }
Ejemplo n.º 2
0
    $app->response->setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Range, Content-Disposition, Content-Type, Authorization');
    /*
     * Add collections for routing
     */
    $collections = glob($config->app->collectionsDir . '*.php');
    foreach ($collections as $collection) {
        $app->mount(include_once $collection);
    }
    /**
     * Handle the request
     */
    $app->notFound(function () use($app) {
        throw new Exception('Not Found', 404);
    });
    $app->after(function () use($app) {
        $data = $app->getReturnedValue();
        $app->response->setContentType('application/json', 'utf-8');
        $app->response->setStatusCode($data['code'], null);
        $app->response->setJsonContent($data['content']);
        $app->response->send();
    });
    $app->handle();
} catch (Exception $e) {
    $code = $e->getCode() ? $e->getCode() : 500;
    $app->response->setStatusCode($code, $e->getMessage());
    $app->response->setContent($e->getMessage());
    $app->response->send();
}
function error_handler($errno, $errStr, $errFile, $errLine)
{
    throw new Exception($errStr . ' in ' . $errFile . ' on line ' . $errLine, 500);
Ejemplo n.º 3
0
<?php

use Phalcon\Mvc\Micro;
use Phalcon\Di\FactoryDefault;
use Phalcon\Http\Response;
$di = new FactoryDefault();
$di->set('response', function () {
    return new Response();
});
$app = new Micro($di);
// Index
$app->get('/', function () use($app) {
    return ['service' => 'Payment', 'version' => '0.0.0'];
});
// Return JSON
$app->after(function () use($app) {
    return $app->response->setContentType('application/json', 'UTF-8')->setJsonContent($app->getReturnedValue())->send();
});
$app->handle();