nesbert
9/13/2012 - 5:57 PM

Phalcon WIP API index.php

Phalcon WIP API index.php

<?php
$di = new \Phalcon\DI\FactoryDefault();

$response = new \Phalcon\Http\Response();
$response->setContentType('application/json', 'utf-8');
$di->set('response', $response);

$app = new Phalcon\Mvc\Micro();
$app->setDI($di);

try {

    // default route
    $app->get('/', function() {
        echo json_encode(array('status' => 'OK', 'messages' => 'hello world'));
    });

    // default not found
    $app->notFound(function() use ($app) {
        $app->getSharedService('response')
            ->setStatusCode(404, null);
        echo json_encode(array('status' => 'ERROR', 'messages' => 'resource not found'));
    });

} catch (\Exception $e) {
    if ($e->getCode()) {
        $response->setStatusCode($e->getCode(), null);
    } else {
        $response->setStatusCode(500, null);
    }
}

$app->handle();
$response->send();