/** * 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); }
/** * 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); }