public function testMicroEvents() { $trace = array(); $eventsManager = new Phalcon\Events\Manager(); $eventsManager->attach('micro', function ($event) use(&$trace) { $trace[$event->getType()] = true; }); $app = new Phalcon\Mvc\Micro(); $app->setEventsManager($eventsManager); $app->map('/blog', function () { }); $app->handle('/blog'); $this->assertEquals($trace, array('beforeHandleRoute' => true, 'beforeExecuteRoute' => true, 'afterExecuteRoute' => true, 'afterHandleRoute' => true)); }
<?php $app = new Phalcon\Mvc\Micro(); //Executed before every route executed //Return false cancels the route execution $app->before(function () use($app) { if ($app['session']->get('auth') == false) { return false; } return true; }); $app->map('/api/robots', function () { return array('status' => 'OK'); }); $app->after(function () use($app) { //This is executed after the route is executed echo json_encode($app->getReturnedValue()); }); $app->finish(function () use($app) { //This is executed when the request has been served });
<?php try { $config = (include __DIR__ . "/config/config.php"); include __DIR__ . "/config/loader.php"; include __DIR__ . "/config/services.php"; $app = new \Phalcon\Mvc\Micro(); $app->setDI($di); $app->map('/', "request")->via(array('GET', 'POST')); $app->handle(); } catch (\Exception $e) { $debug = $config['application']['debug']; $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'; header($protocol . ' 400 Bad Request'); if (isset($debug) && $debug === true) { die(json_encode(array("result" => "failure", "error" => $e->getMessage(), "stack" => $e->getTraceAsString()))); } else { die(json_encode(array("result" => "failure", "error" => "Une erreur s'est produite."))); } } function request() { global $app; $httprequest = new Phalcon\Http\Request(); $datain = $httprequest->get(); $data = array(); foreach ($datain as $key => $value) { $data[strtolower($key)] = $value; } $filter = new \Phalcon\Filter(); $request = $filter->sanitize($data["request"], array("string", "lower"));
$app['db'] = function () { return new \Phalcon\Db\Adapter\Pdo\Mysql(array('dsn' => 'host=localhost;dbname=hello_world;charset=utf8', 'username' => 'benchmarkdbuser', 'password' => 'benchmarkdbpass', 'persistent' => true)); }; // Setting up the view component (seems to be required even when not used) $app['view'] = function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/../views/'); $view->registerEngines(array(".volt" => function ($view, $di) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $volt->setOptions(array("compiledPath" => __DIR__ . "/../compiled-templates/", "compiledExtension" => ".c", "compiledSeparator" => '_')); return $volt; })); return $view; }; $app->map('/json', function () { header("Content-Type: application/json"); echo json_encode(array('message' => 'Hello, World!')); }); // $app->map('/db', function () use($app) { $db = $app['db']; $queries = $app->request->getQuery('queries', null, 1); $worlds = array(); for ($i = 0; $i < $queries; ++$i) { $worlds[] = $db->fetchOne('SELECT * FROM world WHERE id = ' . mt_rand(1, 10000), Phalcon\Db::FETCH_ASSOC); } if ($queries == 1) { $worlds = $worlds[0]; } echo json_encode($worlds); }); // /fortunes
public function testMicroStopMiddlewareClasses() { Phalcon\DI::reset(); $app = new Phalcon\Mvc\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'); $this->assertEquals($middleware->getNumber(), 3); }