/** * Render specified template file with data provided * @param $template_path Template file path (full) * @param array $data Data array * @param bool|true $wrap To be wrapped with main template if true * @return bool|html * @throws FileException if template file does not exist */ public function render($template_path, $data = array(), $wrap = true) { //Launches the appropriate event Service::get('eventManager')->trigger('renderAction', "Render specified template file \"" . $template_path . "\" with data provided"); $data['include'] = function ($controllerName, $actionName, $params) { Helper::dispatch($controllerName, $actionName, $params); }; $data['getRoute'] = function ($route_name, $params = array()) { return Service::get('router')->buildRoute($route_name, $params); }; $data['generateToken'] = function () { echo '<input type="hidden" name="token" value="' . Service::get('session')->get('token') . '"/>'; }; $data['route'] = Registry::getConfig('route'); $data['user'] = Service::get('session')->get('user'); if (!$wrap) { //Gets messages from session message container to display them in view $flush = Service::get('session')->get('messages') ? Service::get('session')->get('messages') : array(); Service::get('session')->del('messages'); } extract($data); //Checks if template file exists if (file_exists($template_path)) { ob_start(); include $template_path; $content = ob_get_contents(); ob_end_clean(); } else { throw new FileException("File " . $template_path . " does not found"); } if ($wrap) { $content = $this->renderMain($content); } return $content; }
public function run() { $router = Service::get('router'); $route = $router->parseRoute($_SERVER['REQUEST_URI']); try { //Checks if route is empty if (!empty($route)) { //Verifies user role if it needs if (array_key_exists('security', $route)) { if (is_null($user = Service::get('session')->get('user')) || !in_array($user->role, $route['security'])) { throw new SecurityException("Access is denied"); } } //Returns Response object $response = Helper::dispatch($route['controller'], $route['action'], $route['params']); } else { throw new HttpNotFoundException("Route does not found", 404); } } catch (SecurityException $e) { Service::get('session')->set('returnUrl', Registry::getConfig('route')['pattern']); $response = new ResponseRedirect(Service::get('router')->buildRoute('login')); } catch (HttpNotFoundException $e) { $response = new Response(Service::get('renderer')->render(Registry::getConfig('error_400'), array('code' => $e->getCode(), 'message' => $e->getMessage()))); } catch (\Exception $e) { $response = new Response(Service::get('renderer')->render(Registry::getConfig('error_500'), array('code' => $e->getCode(), 'message' => $e->getMessage()))); } $response->send(); }
/** * Rendering method * @param $layout file name * @param array $data * @return Response */ public function render($layout, $data = array()) { Service::get('eventManager')->trigger('controllerRender', "Rendering method. Controller \"" . get_class($this) . "\". Layout \"" . $layout . "\""); // The full path to the view for the appropriate controller $fullpath = realpath(Helper::getViewPath(get_class($this)) . $layout . '.php'); $renderer = Service::get('renderer'); $content = $renderer->render($fullpath, $data); return new Response($content); }
/** * Include template, extract variables for template * initialize closures * return rendered content * * @param $pathView * @param null|array $data * * @return string */ private function getRenderBuffer($pathView, $data = null) { $include = Helper::include(); $getRoute = Helper::buildRoute(); $generateToken = Helper::getTokenField(); $flush = Service::get('session')->getFlushMessages(); if (strripos($pathView, 'layout')) { Service::get('session')->delete('flush'); } $user = Service::get('security')->getUser(); $route['_name'] = Service::get('router')->getNameRoute(); if (Service::get('session')->get('validator.data') !== false) { extract(Service::get('session')->get('validator.data')); } if ($data !== null) { extract($data); } ob_start(); include_once $pathView; return ob_get_clean(); }
/** * @param string $pathToKey * * @return null */ private function preparePath(string $pathToKey) { return Helper::arrayGet($this->config, $pathToKey); }
/** * @param $viewName * @param $data * * @return string */ protected function render($viewName, $data) { $pathView = Helper::viewPath($this->nameBundle, $this->nameFolder, $viewName); return new Response(Service::get('render')->render($pathView, $data, true)); }