/** * 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(); }
/** * Displays profile if it possible otherwise redirects to login page * @return \Framework\Response\Response|\Framework\Response\ResponseRedirect */ public function getAction() { if (Service::get('security')->isAuthenticated()) { return $this->render('get.html', array('posts' => Post::findByParams(array('user_id' => Service::get('session')->get('user')->id)))); } //Sets the return page Service::get('session')->set('returnUrl', Registry::getConfig('route')['pattern']); return $this->redirect($this->generateRoute('login'), 'You need to login'); }
/** * Displays the edit window * @param $id * @return \Framework\Response\Response */ public function start_editAction($id) { if (Service::get('security')->isAuthenticated()) { if (Post::find($id)->user_id === Service::get('session')->get('user')->id) { return $this->render('start_edit.html', array('post' => Post::find($id))); } else { return $this->redirect($this->generateRoute('profile'), '"' . Post::find($id)->title . '".' . ' It is not your article'); } } Service::get('session')->set('returnUrl', $this->generateRoute(Registry::getConfig('route')['_name'], array('id' => $id))); return $this->redirect($this->generateRoute('login'), 'You need to login'); }
public static function errorReporting() { //Checks the development mode. If it is 'dev' shows all messages if (Registry::getConfig("mode") === "dev") { ini_set('display_errors', 'On'); ini_set('log_errors', 'Off'); //Otherwise writes the lof files } else { ini_set('display_errors', 'Off'); ini_set('error_log', __DIR__ . '/../../app/error_logs/' . 'errors_' . date("Y_m_d") . '.log'); ini_set('log_errors', 'On'); } }