404 not found management

This commit is contained in:
Etienne Roudeix
2013-09-04 10:02:58 +02:00
parent 9a537b69d3
commit 29bf7d9a92
7 changed files with 106 additions and 3 deletions

View File

@@ -0,0 +1,84 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
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 <eroudeix@openstudio.fr>
*/
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),
);
}
}

View File

@@ -47,6 +47,11 @@
<tag name="kernel.event_subscriber"/>
</service>
<service id="thelia.action.pageNotFound" class="Thelia\Action\PageNotFound">
<argument type="service" id="service_container"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
</config>

View File

@@ -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();
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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());

View File

@@ -0,0 +1,3 @@
<h1>PAGE NOT FOUND</h1>
<a href="{navigate to="index"}">Back Home</a>