public static function newHttp() { $request = new Request(); $request->setHostname($_SERVER["SERVER_NAME"]); $request->setUri($_SERVER["REQUEST_URI"]); $request->setGetParams($_GET); $request->setPostParams($_POST); $request->setRawBody(@file_get_contents('php://input')); return $request; }
public function testChain() { $request = new Request(); $request->setHostname("t.test.local"); $request->setUri("/"); $router = new Router(); $hostnameRouter = new HostnameRouter("t.test.local"); $hostnameRouter->addChild("hello", new StaticRouter("/", "Hello", "world")); $router->addChild("hostname", $hostnameRouter); $route = $router->match($request); $this->assertEquals("Hello", $route->getControllerName()); }
protected function getLocalResponse($url) { try { $client = new Client($url, array(CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 5, CURLOPT_TIMEOUT => 5)); $request = new Request(); $request->setUri($url); $client->setRequest($request); $response = $client->dispatch($request); if ($response->getStatusCode() == 200) { return json_decode($response->getBody()); } else { throw new Exception('Source returned code ' . $response->getStatusCode()); } } catch (Exception $e) { throw new GushException($e->getMessage(), GushException::Data); } }
/** * HTTP POST METHOD (static) * * @param string $url * @param array $params * @param array $headers * @param mixed $body * @param array|Traversable $clientOptions * @throws Exception\InvalidArgumentException * @return Response|bool */ public static function post($url, $params, $headers = [], $body = null, $clientOptions = null) { if (empty($url)) { return false; } $request = new Request(); $request->setUri($url); $request->setMethod(Request::METHOD_POST); if (!empty($params) && is_array($params)) { $request->getPost()->fromArray($params); } else { throw new Exception\InvalidArgumentException('The array of post parameters is empty'); } if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = Client::ENC_URLENCODED; } if (!empty($headers) && is_array($headers)) { $request->getHeaders()->addHeaders($headers); } if (!empty($body)) { $request->setContent($body); } return static::getStaticClient($clientOptions)->send($request); }
*/ /** * Includes the side wide settings. */ require_once 'vendor/autoload.php'; header("Access-Control-Allow-Origin: *"); // enable CORS for the whole REST API try { $config = new GlobalConfig(); $model = new Model($config); $controller = new RestController($model); $request = new Request($model); $path = $request->getServerConstant('PATH_INFO') ? $request->getServerConstant('PATH_INFO') : ''; // eg. "/search" $parts = explode('/', $path); $request->setUri($request->getQueryParam('uri')); $request->setLang($request->getQueryParam('lang')); if ($request->getQueryParam('vocab')) { $request->setVocab($request->getQueryParam('vocab')); } if (sizeof($parts) < 2 || $parts[1] == "") { header("HTTP/1.0 404 Not Found"); echo "404 Not Found"; } elseif ($parts[1] == 'vocabularies') { $controller->vocabularies($request); } elseif ($parts[1] == 'search') { $controller->search($request); } elseif ($parts[1] == 'types') { $controller->types($request); } elseif ($parts[1] == 'data') { $controller->data($request);
/** * Processing routes */ private function getRoute() { $route = array(); include_once _SYSDIR_ . 'system/Routes.php'; $router = null; $uri = mb_strtolower(trim(_URI_, '/')); $uriPos = mb_strpos($uri, '?'); if ($uriPos !== false) { $uri = mb_substr($uri, 0, $uriPos); } // Проверка заданых путей foreach ($route as $value) { if (preg_match($value['pattern'], $uri, $matches)) { $router['options'] = array(); $router['uri'] = array(); $uri = trim(mb_substr($uri, mb_strlen($matches[0])), '/'); unset($matches[0]); $router['controller'] = $value['controller']; $router['action'] = $value['action']; $router['options'] = array_values($matches); if (!empty($uri)) { $router['uri'] = explode('/', $uri); } break; } } // Если нет заданых путей(Routes) if (!is_array($router)) { $uriArray = explode('/', $uri); $uriCount = count($uriArray); $router['options'] = array(); $router['uri'] = array(); if ($uriCount < 3) { if (!empty($uriArray[0])) { $router['controller'] = $uriArray[0]; } else { $router['controller'] = DEFAULT_CONTROLLER; } if (!empty($uriArray[1])) { $router['action'] = $uriArray[1]; } else { $router['action'] = DEFAULT_ACTION; } } else { $router['controller'] = $uriArray[0]; $router['action'] = $uriArray[1]; unset($uriArray[0]); unset($uriArray[1]); $router['uri'] = array_values($uriArray); } } // Delete array route unset($route); define('CONTROLLER', $router['controller']); define('ACTION', $router['action']); if (is_array($router['options'])) { Request::setUriOptions($router['options']); } if (is_array($router['uri'])) { Request::setUri($router['uri']); } $this->router = $router; }
public function testErrorPages() { $request = new Request(); $request->setUri("/invalid/controller"); ob_start(); $this->object->run($request); $errorPage = ob_get_contents(); ob_end_clean(); $this->assertEquals("--> error action <--", $errorPage); }