From 29bf7d9a92cef5142a3f4c7ec497bdb3172b3864 Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Wed, 4 Sep 2013 10:02:58 +0200 Subject: [PATCH] 404 not found management --- core/lib/Thelia/Action/PageNotFound.php | 84 +++++++++++++++++++ core/lib/Thelia/Config/Resources/action.xml | 5 ++ core/lib/Thelia/Controller/BaseController.php | 5 +- .../Core/Template/Smarty/SmartyParser.php | 1 + core/lib/Thelia/Model/ConfigQuery.php | 10 +++ install/insert.sql | 1 + templates/default/404.html | 3 + 7 files changed, 106 insertions(+), 3 deletions(-) create mode 100755 core/lib/Thelia/Action/PageNotFound.php create mode 100644 templates/default/404.html diff --git a/core/lib/Thelia/Action/PageNotFound.php b/core/lib/Thelia/Action/PageNotFound.php new file mode 100755 index 000000000..c6f547fc4 --- /dev/null +++ b/core/lib/Thelia/Action/PageNotFound.php @@ -0,0 +1,84 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Action; + +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; +use Symfony\Component\HttpKernel\KernelEvents; +use Thelia\Model\ConfigQuery; + +/** + * + * Class PageNotFound + * @package Thelia\Action + * @author Etienne Roudeix + */ +class PageNotFound extends BaseAction implements EventSubscriberInterface +{ + public function display404(GetResponseForExceptionEvent $event) + { + if(is_a($event->getException(), 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException')) { + + $parser = $this->container->get("thelia.parser"); + + // Define the template thant shoud be used + $parser->setTemplate(ConfigQuery::getActiveTemplate()); + + //$event->getRequest()->attributes->set('_view', ConfigQuery::getPageNotFoundView()); + + $response = new Response($parser->render(ConfigQuery::getPageNotFoundView()), 404); + + $event->setResponse($response); + } + } + + /** + * Returns an array of event names this subscriber wants to listen to. + * + * The array keys are event names and the value can be: + * + * * The method name to call (priority defaults to 0) + * * An array composed of the method name to call and the priority + * * An array of arrays composed of the method names to call and respective + * priorities, or 0 if unset + * + * For instance: + * + * * array('eventName' => 'methodName') + * * array('eventName' => array('methodName', $priority)) + * * array('eventName' => array(array('methodName1', $priority), array('methodName2')) + * + * @return array The event names to listen to + * + * @api + */ + public static function getSubscribedEvents() + { + return array( + KernelEvents::EXCEPTION => array("display404", 128), + ); + } +} diff --git a/core/lib/Thelia/Config/Resources/action.xml b/core/lib/Thelia/Config/Resources/action.xml index bcc162e24..7eea2e005 100755 --- a/core/lib/Thelia/Config/Resources/action.xml +++ b/core/lib/Thelia/Config/Resources/action.xml @@ -47,6 +47,11 @@ + + + + + diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index f63324437..a15226e83 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -205,11 +205,10 @@ class BaseController extends ContainerAware /** * Return a 404 error - * - * @return \Symfony\Component\HttpFoundation\Response + * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ protected function pageNotFound() { - + throw new NotFoundHttpException(); } } diff --git a/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php b/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php index 12014e817..89c7eb457 100755 --- a/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php +++ b/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php @@ -36,6 +36,7 @@ class SmartyParser extends Smarty implements ParserInterface /** * @param Request $request * @param EventDispatcherInterface $dispatcher + * @param ParserContext $parserContext * @param bool $template * @param string $env * @param bool $debug diff --git a/core/lib/Thelia/Model/ConfigQuery.php b/core/lib/Thelia/Model/ConfigQuery.php index 4165f4f7c..87b506e93 100755 --- a/core/lib/Thelia/Model/ConfigQuery.php +++ b/core/lib/Thelia/Model/ConfigQuery.php @@ -32,4 +32,14 @@ class ConfigQuery extends BaseConfigQuery { { return self::read("rewriting_enable") == 1; } + + public static function getPageNotFoundView() + { + return self::read("page_not_found_view", '404.html'); + } + + public static function getActiveTemplate() + { + return self::read('active-template', 'default'); + } } // ConfigQuery diff --git a/install/insert.sql b/install/insert.sql index 41c63e353..780be7ae4 100755 --- a/install/insert.sql +++ b/install/insert.sql @@ -14,6 +14,7 @@ INSERT INTO `config` (`name`, `value`, `secured`, `hidden`, `created_at`, `updat ('original_image_delivery_mode', 'symlink', 1, 0, NOW(), NOW()), ('images_library_path', 'local/media/images', 1, 0, NOW(), NOW()), ('image_cache_dir_from_web_root', 'cache/images', 1, 0, NOW(), NOW()); +('page_not_found_view', '404.html', 0, 0, NOW(), NOW()); INSERT INTO `module` (`code`, `type`, `activate`, `position`, `created_at`, `updated_at`) VALUES ('test', '1', '1', '1', NOW(), NOW()); diff --git a/templates/default/404.html b/templates/default/404.html new file mode 100644 index 000000000..7e2e925b4 --- /dev/null +++ b/templates/default/404.html @@ -0,0 +1,3 @@ +

PAGE NOT FOUND

+ +Back Home \ No newline at end of file