/** * This is where our application gets a request and converts it into a response * that is returned to the user * * @return void */ public function run() { $uri = $this->container->get('request')->getUri(); $method = $this->container->get('request')->getMethod(); try { //First we're trying to get a route matching the requested URI $matchingRoute = $this->container->get('router')->getMatchingRoute($uri, $method); //If we've got one we get the parameters from the URI $params = $this->container->get('router')->getRouteParams($matchingRoute, $uri); //And we call the controller linked to the route $response = $this->dispatch($matchingRoute, $params); } catch (\Exception $e) { //very basic Exception management //custom Exceptions would be better $content = array('message' => $e->getMessage()); $headers = $this->container->get('request')->getHeaders(); $response = $this->container->get('responseFormatter')->format($content, $headers['Accept']); $response->setStatus(Response::INTERNAL_ERROR_CODE); } //Now that we have a response from the controller or the Exception Handler //building the HTTP response http_response_code($response->getStatus()); $headers = $response->getHeaders(); //adding headers if ($headers && is_array($headers)) { foreach ($headers as $header) { header($header); } } //And finally we return our response echo $response->getContent(); }
/** * Parse the given template with the given parameters using the template engine * * @param string $templatePath the path to the template file we want to parse * @param params $params the array containing the variable to fill in the template * * @return string the parsed template */ public function render($templatePath, $params) { return new Response($this->container->get('template')->render($templatePath, $params)); }