/* * 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); }
public function testMicroStopMiddlewareClasses() { $this->specify("Micro middleware events don't work as expected", function () { $app = new Micro(); $app->map("/api/site", function () { return true; }); $middleware = new \MyMiddlewareStop(); $app->before($middleware); $app->before($middleware); $app->after($middleware); $app->after($middleware); $app->finish($middleware); $app->finish($middleware); $app->handle("/api/site"); expect($middleware->getNumber())->equals(3); }); }
//Register Directories $loader = new \Phalcon\Loader(); $loader->registerDirs(array(__DIR__ . '/app/models/', __DIR__ . '/app/controllers/', __DIR__ . '/app/classes/'))->register(); // Use composer autoloader to load vendor classes require_once __DIR__ . '/vendor/autoload.php'; //Create the app $app = new Micro(); // Mount the routes $routes = (include_once __DIR__ . '/app/config/routes.php'); foreach ($routes as $route) { $app->mount($route); } // Default Response $app->get('/', function () { return Rs::p(1, 'API is up!'); }); //Add any filter before running the route $app->before(function () use($app) { //You may want to add some basic auth in order to access the REST API }); //This is executed after running the route $app->after(function () use($app) { }); // Not Found $app->notFound(function () use($app) { return Rs::p(0, 'Not Found', [], 404); }); $app->handle(); } catch (\Exception $e) { return Rs::p(0, 'There was an error processing your request', $e->getMessage(), 400); }
<?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();