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)); }
// Mount Collections require __DIR__ . "/../app/bootstrap/collections.php"; // Other routes $app->get('/', function () use($app) { /** @var Phalcon\Mvc\View\Simple $view */ $view = $app->di->get(AppServices::VIEW); return $view->render('general/index'); }); $app->get('/proxy.html', function () use($app, $config) { /** @var Phalcon\Mvc\View\Simple $view */ $view = $app->di->get(AppServices::VIEW); $view->setVar('client', $config->clientHostName); return $view->render('general/proxy'); }); // Start application $app->handle(); // Set content $returnedValue = $app->getReturnedValue(); if ($returnedValue !== null) { if (is_string($returnedValue)) { $app->response->setContent($returnedValue); } else { $app->response->setJsonContent($returnedValue); } } $response = $app->response; } catch (Exception $e) { $response = $di->get(AppServices::RESPONSE); $response->setErrorContent($e, $application_env == 'development'); } // Send response
public function testMicroCollectionsLazy() { Phalcon\DI::reset(); $app = new Phalcon\Mvc\Micro(); $collection = new Phalcon\Mvc\Micro\Collection(); $collection->setHandler('PersonasLazyController', true); $collection->map('/', 'index'); $collection->map('/edit/{number}', 'edit'); $app->mount($collection); $app->handle('/'); $this->assertEquals(PersonasLazyController::getEntered(), 1); $app->handle('/edit/100'); $this->assertEquals(PersonasLazyController::getEntered(), 101); }
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); }
/** * Tests the notFound * * @issue T169 * @author Nikos Dimopoulos <*****@*****.**> * @since 2012-11-06 */ public function testMicroNotFound_T169() { $handler = new RestHandler($this); $app = new \Phalcon\Mvc\Micro(); $app->get('/api/site', array($handler, 'find')); $app->post('/api/site/save', array($handler, 'save')); $flag = false; $app->notFound(function () use(&$flag) { $flag = true; }); $_SERVER['REQUEST_METHOD'] = 'GET'; $_GET['_url'] = '/fourohfour'; $app->handle(); $this->assertTrue($flag); }
/*Incluimos las instrucciones para la tabla Entidadpedido*/ include __DIR__ . "/../app/rest/Entidadpedido.php"; /*Incluimos las instrucciones para la tabla Factura*/ include __DIR__ . "/../app/rest/Factura.php"; /*Incluimos las instrucciones para la tabla Lineafactura*/ include __DIR__ . "/../app/rest/Lineafactura.php"; /*Incluimos las instrucciones para la tabla Entidadfactura*/ include __DIR__ . "/../app/rest/Entidadfactura.php"; /*Incluimos las instrucciones para la tabla Albaran*/ include __DIR__ . "/../app/rest/Albaran.php"; /*Incluimos las instrucciones para la tabla Lineaalbaran*/ include __DIR__ . "/../app/rest/Lineaalbaran.php"; /*Incluimos las instrucciones para la tabla Entidadalbaran*/ include __DIR__ . "/../app/rest/Entidadalbaran.php"; /*Incluimos las instrucciones para la tabla Pedidofacturaalbaran*/ include __DIR__ . "/../app/rest/Pedidofacturaalbaran.php"; /*Incluimos las instrucciones para la tabla Nomina*/ include __DIR__ . "/../app/rest/Nomina.php"; /*Incluimos las instrucciones para la tabla Lineanomina*/ include __DIR__ . "/../app/rest/Lineanomina.php"; /*Incluimos las instrucciones para la tabla Entidadnomina*/ include __DIR__ . "/../app/rest/Entidadnomina.php"; /*Incluimos las instrucciones para la tabla Librocontabilidad*/ include __DIR__ . "/../app/rest/Librocontabilidad.php"; /*Incluimos las instrucciones para la tabla Linealibrocontabilidad*/ include __DIR__ . "/../app/rest/Linealibrocontabilidad.php"; // echo $application->handle()->getContent(); $application->handle(); } catch (\Exception $e) { echo $e->getMessage(); }
$app->put('/ajax/settings', function () use($app) { $_uid = (new userSessions())->requiredLogin(true); $data = $app->request->getJsonRawBody(); if ((new users())->emailExists($data->auth->email, $_uid)) { $app->response->setJsonContent(array('alerts' => array('message' => 'The email already exists on socialSample account.', 'type' => 'danger'))); $app->response->send(); return false; } if ((new profiles())->usernameExists($data->profile->username, $_uid)) { $app->response->setJsonContent(array('alerts' => array('message' => 'The username already exists on socialSample account.', 'type' => 'danger'))); $app->response->send(); return false; } //required to set them lowercase $data->profile->username = strtolower($data->profile->username); $data->auth->email = strtolower($data->auth->email); $profile = (new profiles())->_set($_uid, $data->profile); $auth = (new users())->_set($_uid, $data->auth); if ($profile && $auth) { $authData['auth'] = (new users())->_get($_uid); $profileData['profile'] = (new profiles())->_get($_uid); $privacyData['privacy'] = (new privacy())->_get($_uid); $alertData['alerts'] = array('message' => 'All changes are saved', 'type' => 'success'); } $app->response->setJsonContent(array_merge($authData, $profileData, $privacyData, $alertData)); $app->response->send(); //return json_encode(array_merge($authData, $profileData, $privacyData), JSON_PRETTY_PRINT); //save all data }); echo $app->handle();
$security->setWorkFactor(12); return $security; }, true); $container->set('db', function () { return new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => 'localhost', "username" => 'root', "password" => 'root', "dbname" => 'dez-auth')); }); $app = new \Phalcon\Mvc\Micro(); $app->setDI($container); $app->session->start(); $app->get('/', function () use($container, $app) { /** @var \PhalconDez\Auth\Auth $auth */ $auth = $container->get('auth'); $email = '*****@*****.**'; $password = '******'; try { $auth->authenticate($email, $password); } catch (\Exception $e) { $auth->create($email, $password); $container->get('response')->redirect('auth-page'); } if ($app->request->get('auth') > 0) { $auth->authenticate('*****@*****.**', '123qwe'); } if ($app->request->get('create') > 0) { $auth->create('*****@*****.**', '123qwe'); } var_dump($auth); return new \Phalcon\Http\Response('test auth component'); }); $app->handle()->getContent();
/** * Handles the request. */ public function main() { /** * Our application is a Micro application, so we must explicitly define all the routes. * For APIs, this is ideal. This is as opposed to the more robust MVC Application * @var $app */ $app = new \Phalcon\Mvc\Micro(); $app->setDI($this->di); /** * This will require changes to fit your application structure. * It supports Auth, Session auth, and Exempted routes. * It also allows all Options requests, as those tend to not come with * cookies or basic auth credentials and Preflight is not implemented the * same in every browser. */ $app->before(function () use($app) { // Oauth, for programmatic responses if ($app->request->getHeader('X_COMPARE_REST_API_KEY') && $app->request->get('language') && $app->request->get('countryCode')) { $session = new SessionsController(); $result = $session->resource($app->request->getHeader('X_COMPARE_REST_API_KEY')); if ($result) { return true; } else { throw new HTTPException('Invalid access token.', 401, ['dev' => 'Please provide credentials by passing your access token.', 'internalCode' => 'Unauth:1']); } } // If we made it this far, we have no valid auth method, throw a 401. throw new HTTPException('Must provide credentials.', 401, ['dev' => 'Please provide credentials by passing your access token, language and country code.', 'internalCode' => 'Unauth:1']); return false; }); /** * Mount all of the collections, which makes the routes active. */ foreach ($this->di->getShared('collections') as $collection) { $app->mount($collection); } /** * The base route return the list of defined routes for the application. * This is not strictly REST compliant, but it helps to base API documentation off of. * By calling this, you can quickly see a list of all routes and their methods. */ $app->get('/', function () use($app) { $routes = $app->getRouter()->getRoutes(); $routeDefinitions = array('GET' => array(), 'POST' => array(), 'PUT' => array(), 'PATCH' => array(), 'DELETE' => array(), 'HEAD' => array(), 'OPTIONS' => array()); foreach ($routes as $route) { $method = $route->getHttpMethods(); $routeDefinitions[$method][] = $route->getPattern(); } return $routeDefinitions; }); /** * After a route is run, usually when its Controller returns a final value, * the application runs the following function which actually sends the response to the client. * * The default behavior is to send the Controller's returned value to the client as JSON. * However, by parsing the request querystring's 'type' paramter, it is easy to install * different response type handlers. Below is an alternate csv handler. */ $app->after(function () use($app) { // OPTIONS have no body, send the headers, exit if ($app->request->getMethod() == 'OPTIONS') { $app->response->setStatusCode('200', 'OK'); $app->response->send(); return; } // Respond by default as JSON if (!$app->request->get('type') || 'json' == $app->request->get('type') || 'option' == $app->request->get('type')) { // Results returned from the route's controller. All Controllers should return an array $records = $app->getReturnedValue(); $response = new JSONResponse(); $response->useEnvelope(true)->convertSnakeCase(true)->send($records); return; } else { if ('xml' == $app->request->get('type')) { $records = $app->getReturnedValue(); $response = new XMLResponse(); $response->send($records); return; } else { if ('csv' == $app->request->get('type')) { $records = $app->getReturnedValue(); $response = new CSVResponse(); $response->useHeaderRow(true)->send($records); return; } else { throw new HTTPException('Could not return results in specified format', 403, array('dev' => 'Could not understand type specified by type paramter in query string.', 'internalCode' => 'NF1000', 'more' => 'Type may not be implemented. Choose either "json", "xml" or "csv"')); } } } }); /** * The notFound service is the default handler function that runs when no route was matched. * We set a 404 here unless there's a suppress error codes. */ $app->notFound(function () use($app) { throw new HTTPException('Not Found.', 404, array('dev' => 'That route was not found on the server.', 'internalCode' => 'NF1000', 'more' => 'Check route for mispellings.')); }); /** * If the application throws an HTTPException, send it on to the client as json. * Elsewise, just log it. */ set_exception_handler(function ($exception) use($app) { //HTTPException's send method provides the correct response headers and body if (is_a($exception, 'App\\Common\\Lib\\Application\\Exceptions\\HTTPException')) { $exception->send(); } error_log($exception); error_log($exception->getTraceAsString()); }); $app->handle(); }