/** * Render the not found page if on frontend and http exception * * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); if (!$exception instanceof HttpExceptionInterface || Zone::isBackend($event->getRequest())) { return; } if ($exception->getStatusCode() !== Response::HTTP_NOT_FOUND) { return; } // If $notFoundPage is referencing a template, render it and be done. if ($this->render->hasTemplate($this->notFoundPage)) { try { $this->renderNotFound($event, $this->notFoundPage, []); } catch (TwigErrorLoader $e) { // Template not found, fall though to see if we can render a // record, failing that let the exception handler take over } } // Next try for referencing DB content. $content = $this->storage->getContent($this->notFoundPage, ['returnsingle' => true]); if (!$content instanceof Content || empty($content->id)) { return; } $template = $this->templateChooser->record($content); $this->renderNotFound($event, $template, $content->getTemplateContext()); }
/** * Callback for reponse event. * * @param FilterResponseEvent $event */ public function onResponse(FilterResponseEvent $event) { if (!$event->isMasterRequest()) { return; } $response = $event->getResponse(); if (strpos($response->headers->get('Content-Type'), 'text/html') === false) { return; } $this->addSnippets(); $response->setContent($this->render->postProcess($response)); }
/** * Render the not found page if on frontend and http exception * * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { if (!$event->getException() instanceof HttpExceptionInterface || Zone::isBackend($event->getRequest())) { return; } $content = $this->storage->getContent($this->notFoundPage, ['returnsingle' => true]); if (!$content instanceof Content || empty($content->id)) { return; } $template = $this->templateChooser->record($content); $response = $this->render->render($template, $content->getTemplateContext()); $event->setResponse($response); }
/** * Handle errors thrown in the application. * * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); // Log the error message $message = $exception->getMessage(); $this->logger->critical($message, ['event' => 'exception', 'exception' => $exception]); if ($exception instanceof HttpExceptionInterface && !Zone::isBackend($event->getRequest())) { $message = "The page could not be found, and there is no 'notfound' set in 'config.yml'. Sorry about that."; } $context = ['class' => get_class($exception), 'message' => $message, 'code' => $exception->getCode(), 'trace' => $this->getSafeTrace($exception)]; // Note: This uses the template from app/theme_defaults. Not app/view/twig. $response = $this->render->render('error.twig', ['context' => $context]); $event->setResponse($response); }
/** * Callback for reponse event. * * @param FilterResponseEvent $event */ public function onResponse(FilterResponseEvent $event) { if (!$event->isMasterRequest()) { return; } $request = $event->getRequest(); if (Zone::isAsync($request)) { return; } $response = $event->getResponse(); if (strpos($response->headers->get('Content-Type'), 'text/html') === false) { return; } if (!$event->getRequest()->isXmlHttpRequest()) { $this->addSnippets(); } $this->render->postProcess($request, $response); }
/** * Insert a snippet of Javascript to fetch the actual widget's contents. * * @param WidgetAssetInterface $widget * @param Response $response */ protected function addDeferredJavaScript(WidgetAssetInterface $widget, Response $response) { if ($this->deferAdded) { return; } $javaScript = $this->render->render('widgetjavascript.twig', ['widget' => $widget]); $snippet = (new Snippet())->setLocation(Target::AFTER_BODY_JS)->setCallback((string) $javaScript); $this->deferAdded = true; $this->injector->inject($snippet, Target::AFTER_BODY_JS, $response); }
/** * Render the not found page if on frontend and http exception * * @param GetResponseForExceptionEvent $event */ public function onKernelException(GetResponseForExceptionEvent $event) { if (!$event->getException() instanceof HttpExceptionInterface || Zone::isBackend($event->getRequest())) { return; } // If $notFoundPage is referencing a template, render it and be done. if ($this->render->hasTemplate($this->notFoundPage)) { $response = $this->render->render($this->notFoundPage); $event->setResponse($response); return; } // Next try for referencing DB content. $content = $this->storage->getContent($this->notFoundPage, ['returnsingle' => true]); if (!$content instanceof Content || empty($content->id)) { return; } $template = $this->templateChooser->record($content); $response = $this->render->render($template, [], $content->getTemplateContext()); $event->setResponse($response); }