className = substr(array_pop($class), 0, -10); $this->setControllerView(); } /** * Set the view that this controller should use. */ protected function setControllerView() { if (View::exists($this->className)) { $this->controllerView = new View($this->className); } else { $this->controllerView = new View\Template('{@content}'); } } /** * Set the view that this controller action should use. * @param $action */ protected function setView($action) { if (View::exists($this->className . '/' . $action)) { $this->view = new View($this->className . '/' . $action); } } /** * Handle the incoming request. * @param $action * @param $actionParams * * @return Response */ public function handleAction($action, $actionParams) { $this->setView($action); $response = parent::handleAction($action, $actionParams); if ($response instanceof Response) { return $response; } if (is_string($response)) { $this->controllerView->content = $response; } elseif (isset($this->view)) { $this->controllerView->content = $this->view->render(); } $this->response->setContent($this->controllerView->render()); return $this->response; } /** * Require that the currently logged in user is an administrator. * @throws ForbiddenException */ protected function requireAdmin() { if (!$this->currentUserIsAdmin()) { throw new ForbiddenException('You do not have permission to do that.'); } } /** * Check if the currently logged in user is an administrator. * @return bool */ protected function currentUserIsAdmin() { return $_SESSION['php-censor-user']->getIsAdmin(); } }