/** * Gets a raw response from the internal API * This will trick Phalcon into thinking its a * real PUT, POST, PATCH, GET or DELETE request * It will override the Default DI (Which will be the current site) * and will restore everything after the request * It seems hacky, but I am not sure if there is any better way, please * submit a pull request if you can improve! :) * todo test if the below does in fact require a new instance of DI * if it is being called from within the API * * @param $method * @param $path * @param RequestOptions $options * * @return Response */ private function getRawResponse($method, $path, RequestOptions $options = null) { // Backup super globals $request = $_REQUEST; $post = $_POST; $get = $_GET; // Override the request params $_GET = $options ? $options->getGetParams() : []; $_POST = $options ? $options->getPostParams() : []; // Set HTTP method in GET $_GET['method'] = $method; $_GET['_url'] = $path; $_REQUEST = ['type' => 'raw']; // todo is this requred? // Get current DI $defaultDI = DI::getDefault(); if ($defaultDI instanceof PhrestDI) { $apiDI = $defaultDI; } else { // Set API DI to the default, this is required for models etc. // As Phalcon will get the default DI to perform actions $apiDI = self::getInstance()->app->getDI(); DI::setDefault($apiDI); } // Cache the URI & method self::$uri = $path; self::$method = $method; // Get response from API // todo post not picked up try { $response = $this->app->handle($path); } catch (\Exception $e) { DI::setDefault($defaultDI); throw $e; } // Remove cached uri & method self::$uri = null; self::$method = null; // Restore default DI if (!$defaultDI instanceof PhrestDI) { DI::setDefault($defaultDI); } // Restore super globals $_REQUEST = $request; $_POST = $post; $_GET = $get; return $response; }
<?php /** * Phalcon Skeleton REST API Project: Front Controller */ use Phrest\API\PhrestAPI; use Phrest\Skeleton\Common\APIDI; // Include the composer autoloader require dirname(__DIR__) . '/vendor/autoload.php'; // Handle the request $di = new APIDI(); $app = new PhrestAPI($di); $app->handle();