Skip to content

Instantly share code, notes, and snippets.

@albulescu
Last active August 29, 2015 14:06
Show Gist options
  • Save albulescu/b8b84373e6a215694cb3 to your computer and use it in GitHub Desktop.
Save albulescu/b8b84373e6a215694cb3 to your computer and use it in GitHub Desktop.
<?php
use Phalcon\Mvc\Micro,
Phalcon\Events\Manager as EventsManager,
Phalcon\Http\Response,
Phalcon\Http\Request;
//Create a events manager
$eventManager = new EventsManager();
$request = new Request();
$debug = $request->getQuery('_debug', null, false) == 'true';
//Listen all the application events
$eventManager->attach('dispatch:beforeException', function($event, $app) {
});
$app = new Micro();
$app->setEventsManager($eventManager);
$app->get('/welcome/{name}', function ( $name ) {
throw new \Exception('a');
});
$app->after(function() use ($app) {
$app->response->setContentType('application/json')->sendHeaders();
echo json_encode(array(
'status' => 'success',
'data' => $app->getReturnedValue()
));
});
try
{
$app->handle();
}
catch( \Exception $e ) {
$response = new Response();
$error = array(
'status' => 'error',
'code' => $e->getCode()
);
if( $debug ) {
$error['message'] = $e->getMessage();
}
$response->setStatusCode('500', 'Internal error');
$response->setContentType('application/json');
$response->setJsonContent($error);
$response->send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment