Last active
August 29, 2015 14:06
-
-
Save albulescu/b8b84373e6a215694cb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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