/** * Denied access * @throws ForbiddenException * @return void */ public function denied() { // add messages make sense only if presentation is not json, xml, etc if (!$this->getResponse()->getPresentation()) { Messages::addError('You don\'t have permissions, please sign in'); } // redirect to login page if (!$this->user()) { // save URL to session and redirect make sense if presentation is null if (!$this->getResponse()->getPresentation()) { Session::set('rollback', Request::getRequestUri()); $this->redirectTo('users', 'signin'); } } throw new ForbiddenException(); }
/** * Do process * @return void */ protected function doProcess() { Logger::info("app:process:do"); $module = Request::getModule(); $controller = Request::getController(); $params = Request::getAllParams(); // try to dispatch controller try { // get reflection of requested controller $controllerFile = $this->getControllerFile($module, $controller); $reflection = $this->reflection($controllerFile); // check header "accept" for catch JSON(P) or XML requests, and switch presentation // it's some magic for AJAX and REST requests if ($produces = $reflection->getAccept() and $accept = $this->getRequest()->getAccept()) { // switch statement for $accept switch ($accept) { case Request::ACCEPT_HTML: // with layout // without additional presentation break; case Request::ACCEPT_JSON: $this->useJson(); break; case Request::ACCEPT_JSONP: $this->useJsonp(); break; case Request::ACCEPT_XML: $this->useXml(); break; default: // not acceptable MIME type throw new NotAcceptableException(); break; } } // check call method(s) if ($reflection->getMethod() && !in_array(Request::getMethod(), $reflection->getMethod())) { throw new NotAllowedException(join(',', $reflection->getMethod())); } // check HTML cache if ($reflection->getCacheHtml() && Request::getMethod() == Request::METHOD_GET) { $htmlKey = 'html:' . $module . ':' . $controller . ':' . http_build_query($params); if ($cachedHtml = Cache::get($htmlKey)) { Response::setBody($cachedHtml); return; } } // dispatch controller $dispatchResult = $this->dispatch($module, $controller, $params); } catch (RedirectException $e) { Response::setException($e); if (Request::isXmlHttpRequest()) { // 204 - No Content Response::setStatusCode(204); Response::setHeader('Bluz-Redirect', $e->getMessage()); } else { Response::setStatusCode($e->getCode()); Response::setHeader('Location', $e->getMessage()); } return null; } catch (ReloadException $e) { Response::setException($e); if (Request::isXmlHttpRequest()) { // 204 - No Content Response::setStatusCode(204); Response::setHeader('Bluz-Reload', 'true'); } else { Response::setStatusCode($e->getCode()); Response::setHeader('Refresh', '0; url=' . Request::getRequestUri()); } return null; } catch (\Exception $e) { Response::setException($e); // cast to valid HTTP error code // 500 - Internal Server Error $statusCode = 100 <= $e->getCode() && $e->getCode() <= 505 ? $e->getCode() : 500; Response::setStatusCode($statusCode); $dispatchResult = $this->dispatch(Router::getErrorModule(), Router::getErrorController(), array('code' => $e->getCode(), 'message' => $e->getMessage())); } if ($this->hasLayout()) { Layout::setContent($dispatchResult); $dispatchResult = Layout::getInstance(); } if (isset($htmlKey, $reflection)) { // @TODO: Added ETag header Cache::set($htmlKey, $dispatchResult(), $reflection->getCacheHtml()); Cache::addTag($htmlKey, $module); Cache::addTag($htmlKey, 'html'); Cache::addTag($htmlKey, 'html:' . $module); Cache::addTag($htmlKey, 'html:' . $module . ':' . $controller); } Response::setBody($dispatchResult); }
* * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\View\Helper; use Bluz\View\View; use Bluz\Proxy\Request; use Bluz\Proxy\Translator; return function ($text, $href, array $attributes = []) { // if href is settings for url helper if (is_array($href)) { $href = $this->url(...$href); } // href can be null, if access is denied if (null === $href) { return ''; } if ($href == Request::getRequestUri()) { if (isset($attributes['class'])) { $attributes['class'] .= ' on'; } else { $attributes['class'] = 'on'; } } $attributes = $this->attributes($attributes); return '<a href="' . $href . '" ' . $attributes . '>' . Translator::translate($text) . '</a>'; };
/** * Get the request URI without baseUrl * * @return string */ public function getCleanUri() { if ($this->cleanUri === null) { $uri = parse_url(Request::getRequestUri()); $uri = $uri['path']; if ($this->getBaseUrl() && strpos($uri, $this->getBaseUrl()) === 0) { $uri = substr($uri, strlen($this->getBaseUrl())); } $this->cleanUri = $uri; } return $this->cleanUri; }
* Example of static route * * @author Anton Shevchuk * @created 12.06.12 13:08 */ namespace Application; use Bluz\Proxy\Layout; use Bluz\Proxy\Request; return function () use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ Layout::breadCrumbs([Layout::ahref('Test', ['test', 'index']), 'Routers Examples']); $uri = Request::getRequestUri(); $module = Request::getModule(); $controller = Request::getController(); echo <<<CODE <h4>URL: {$uri}</h4> <h4>Route: {$module}/{$controller}</h4> <pre> /** * @route /static-route/ * @route /another-route.html * @return \\closure */ </pre> CODE; return false; };