SID : rewriting

This commit is contained in:
Etienne Roudeix
2013-09-03 10:09:51 +02:00
parent 6fa4d3a923
commit 3de03dc876
8 changed files with 216 additions and 4 deletions

View File

@@ -36,4 +36,8 @@
<default key="_controller">Thelia\Controller\Front\CartController::changeItem</default> <default key="_controller">Thelia\Controller\Front\CartController::changeItem</default>
<default key="_view">cart</default> <default key="_view">cart</default>
</route> </route>
<route id="url-rewriting.check" path="/{rewritten_url}" methods="GET">
<default key="_controller">Thelia\Controller\Front\UrlRewritingController::check</default>
</route>
</routes> </routes>

View File

@@ -148,9 +148,9 @@ class BaseController extends ContainerAware
* redirect request to specify url * redirect request to specify url
* @param string $url * @param string $url
*/ */
public function redirect($url) public function redirect($url, $status = 302)
{ {
Redirect::exec($url); Redirect::exec($url, $status);
} }
/** /**

View File

@@ -23,6 +23,9 @@
namespace Thelia\Controller\Front; namespace Thelia\Controller\Front;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Thelia\Model\ConfigQuery;
use Thelia\Tools\Redirect;
use Thelia\Tools\URL;
/** /**
* *
@@ -43,6 +46,16 @@ class DefaultController extends BaseFrontController
*/ */
public function noAction(Request $request) public function noAction(Request $request)
{ {
if(ConfigQuery::isRewritingEnable()) {
/* Does the query GET parameters match a rewritten URL ? */
$rewrittenUrl = URL::retrieveCurrent($request, true);
if($rewrittenUrl !== null) {
/* 301 redirection to rewritten URL */
$this->redirect($rewrittenUrl, 301);
}
}
if (! $view = $request->query->get('view')) { if (! $view = $request->query->get('view')) {
$view = "index"; $view = "index";
if ($request->request->has('view')) { if ($request->request->has('view')) {

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\Controller\Front;
//use Propel\Runtime\Exception\PropelException;
//use Thelia\Form\Exception\FormValidationException;
//use Thelia\Core\Event\CartEvent;
//use Thelia\Core\Event\TheliaEvents;
//use Symfony\Component\HttpFoundation\Request;
//use Thelia\Form\CartAdd;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Exception\UrlRewritingException;
use Thelia\Model\ConfigQuery;
use Thelia\Tools\URL;
class UrlRewritingController extends BaseFrontController
{
public function check(Request $request)
{
if(ConfigQuery::isRewritingEnable()) {
try {
$rewrittentUrlData = URL::resolveCurrent($request);
} catch(UrlRewritingException $e) {
$code = $e->getCode();
switch($e->getCode()) {
case UrlRewritingException::URL_NOT_FOUND :
/* TODO : redirect 404 */
throw $e;
break;
default:
throw $e;
}
}
/* define GET arguments in request */
if(null !== $rewrittentUrlData->view) {
$request->query->set('view', $rewrittentUrlData->view);
if(null !== $rewrittentUrlData->viewId) {
$request->query->set($rewrittentUrlData->view . '_id', $rewrittentUrlData->viewId);
}
}
if(null !== $rewrittentUrlData->locale) {
$request->query->set('locale', $rewrittentUrlData->locale);
}
foreach($rewrittentUrlData->otherParameters as $parameter => $value) {
$request->query->set($parameter, $value);
}
}
if (! $view = $request->query->get('view')) {
$view = "index";
if ($request->request->has('view')) {
$view = $request->request->get('view');
}
}
$request->attributes->set('_view', $view);
}
}

View File

@@ -0,0 +1,40 @@
<?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\Exception;
use Thelia\Log\Tlog;
class UrlRewritingException extends \Exception
{
const UNKNOWN_EXCEPTION = 0;
const URL_NOT_FOUND = 404;
public function __construct($message, $code = null, $previous = null) {
if($code === null) {
$code = self::UNKNOWN_EXCEPTION;
}
parent::__construct($message, $code, $previous);
}
}

View File

@@ -22,6 +22,11 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Rewriting; namespace Thelia\Rewriting;
use Propel\Runtime\ActiveQuery\Criteria;
use Thelia\Exception\RewritingUrlException;
use Thelia\Exception\UrlRewritingException;
use Thelia\Model\RewritingArgumentQuery;
/** /**
* Class RewritingResolver * Class RewritingResolver
* @package Thelia\Rewriting * @package Thelia\Rewriting
@@ -31,5 +36,47 @@ namespace Thelia\Rewriting;
*/ */
class RewritingResolver class RewritingResolver
{ {
public $view;
public $viewId;
public $locale;
public $otherParameters;
public function __construct($url = null)
{
if($url !== null) {
$this->load($url);
}
}
public function load($rewrittenUrl)
{
$search = RewritingArgumentQuery::create()
//->filterByUrl($rewrittenUrl)
->joinRewritingUrl('ru', Criteria::RIGHT_JOIN)
->where('`ru`.URL = ?', $rewrittenUrl, \PDO::PARAM_STR)
->withColumn('`ru`.URL', 'ru_url')
->withColumn('`ru`.VIEW', 'ru_view')
->withColumn('`ru`.VIEW_LOCALE', 'ru_locale')
->withColumn('`ru`.VIEW_ID', 'ru_viewId')
->find();
if($search->count() == 0) {
throw new UrlRewritingException('URL NOT FOUND', UrlRewritingException::URL_NOT_FOUND);
}
$otherParameters = array();
foreach($search as $result) {
$parameter = $result->getParameter();
$value = $result->getValue();
if(null !== $parameter) {
$otherParameters[$parameter] = $value;
}
}
$this->view = $search->getFirst()->getVirtualColumn('ru_view');
$this->viewId = $search->getFirst()->getVirtualColumn('ru_viewId');
$this->locale = $search->getFirst()->getVirtualColumn('ru_locale');
$this->otherParameters = $otherParameters;
}
} }

View File

@@ -79,6 +79,10 @@ class RewritingRetriever
*/ */
public function getSpecificUrl($view, $viewLocale, $viewId = null, $viewOtherParameters = array()) public function getSpecificUrl($view, $viewLocale, $viewId = null, $viewOtherParameters = array())
{ {
if(empty($viewOtherParameters)) {
return $this->getViewUrl($view, $viewLocale, $viewId);
}
$urlQuery = RewritingUrlQuery::create() $urlQuery = RewritingUrlQuery::create()
->joinRewritingArgument('ra', Criteria::LEFT_JOIN) ->joinRewritingArgument('ra', Criteria::LEFT_JOIN)
->withColumn('`ra`.REWRITING_URL_ID', 'ra_REWRITING_URL_ID') ->withColumn('`ra`.REWRITING_URL_ID', 'ra_REWRITING_URL_ID')

View File

@@ -25,6 +25,7 @@ namespace Thelia\Tools;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Thelia\Model\ConfigQuery; use Thelia\Model\ConfigQuery;
use Thelia\Rewriting\RewritingResolver;
use Thelia\Rewriting\RewritingRetriever; use Thelia\Rewriting\RewritingRetriever;
class URL class URL
@@ -126,7 +127,7 @@ class URL
return $rewrittenUrl === null ? self::viewUrl($view, array($view . '_id' => $viewId, 'locale' => $viewLocale)) : $rewrittenUrl; return $rewrittenUrl === null ? self::viewUrl($view, array($view . '_id' => $viewId, 'locale' => $viewLocale)) : $rewrittenUrl;
} }
public static function retrieveCurrent(Request $request) public static function retrieveCurrent(Request $request, $rewrittenUrlOnly = false)
{ {
$rewrittenUrl = null; $rewrittenUrl = null;
if(ConfigQuery::isRewritingEnable()) { if(ConfigQuery::isRewritingEnable()) {
@@ -151,6 +152,25 @@ class URL
$rewrittenUrl = $retriever->getSpecificUrl($view, $viewLocale, $viewId, $allOtherParameters); $rewrittenUrl = $retriever->getSpecificUrl($view, $viewLocale, $viewId, $allOtherParameters);
} }
return $rewrittenUrl === null ? self::viewUrl($view, $allParametersWithoutView) : $rewrittenUrl; if($rewrittenUrlOnly) {
return $rewrittenUrl !== null ? $rewrittenUrl : null;
} else {
return $rewrittenUrl !== null ? $rewrittenUrl : self::viewUrl($view, $allParametersWithoutView);
}
}
public static function resolve($url)
{
$resolver = new RewritingResolver($url);
return $resolver;
}
public static function resolveCurrent(Request $request)
{
if(ConfigQuery::isRewritingEnable()) {
return self::resolve($request->get('rewritten_url'));
}
return null;
} }
} }