- Add Symfony2 routing ability to Thelia router
This commit is contained in:
gmorel
2013-09-09 16:08:12 +02:00
parent 7d4003741c
commit 849520eff9
2 changed files with 40 additions and 12 deletions

View File

@@ -22,6 +22,9 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Controller\Admin; namespace Thelia\Controller\Admin;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Thelia\Controller\BaseController; use Thelia\Controller\BaseController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Thelia\Core\Security\Exception\AuthorizationException; use Thelia\Core\Security\Exception\AuthorizationException;
@@ -211,12 +214,26 @@ class BaseAdminController extends BaseController
/** /**
* Return the route path defined for the givent route ID * Return the route path defined for the givent route ID
* *
* @param string $routeId a route ID, as defines in Config/Resources/routing/admin.xml * @param string $routeId a route ID, as defines in Config/Resources/routing/admin.xml
* @param mixed $parameters An array of parameters
* @param Boolean|string $referenceType The type of reference to be generated (one of the constants)
*
* @throws RouteNotFoundException If the named route doesn't exist
* @throws MissingMandatoryParametersException When some parameters are missing that are mandatory for the route
* @throws InvalidParameterException When a parameter value for a placeholder is not correct because
* it does not match the requirement
* @throws \InvalidArgumentException When the router doesn't exist
* @return string The generated URL
* *
* @see \Thelia\Controller\BaseController::getRouteFromRouter() * @see \Thelia\Controller\BaseController::getRouteFromRouter()
*/ */
protected function getRoute($routeId) { protected function getRoute($routeId, $parameters = array(), $referenceType = Router::ABSOLUTE_PATH) {
return $this->getRouteFromRouter('router.admin', $routeId); return $this->getRouteFromRouter(
'router.admin',
$routeId,
$parameters,
$referenceType
);
} }
/** /**

View File

@@ -25,6 +25,10 @@ namespace Thelia\Controller;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Router;
use Thelia\Core\Security\SecurityContext; use Thelia\Core\Security\SecurityContext;
use Thelia\Tools\URL; use Thelia\Tools\URL;
use Thelia\Tools\Redirect; use Thelia\Tools\Redirect;
@@ -216,20 +220,27 @@ class BaseController extends ContainerAware
/** /**
* Get a route path from the route id. * Get a route path from the route id.
* *
* @param $routerName * @param string $routerName Router name
* @param $routeId * @param string $routeId The name of the route
* @param mixed $parameters An array of parameters
* @param Boolean|string $referenceType The type of reference to be generated (one of the constants)
* *
* @return mixed * @throws RouteNotFoundException If the named route doesn't exist
* @throws InvalidArgumentException * @throws MissingMandatoryParametersException When some parameters are missing that are mandatory for the route
* @throws InvalidParameterException When a parameter value for a placeholder is not correct because
* it does not match the requirement
* @throws \InvalidArgumentException When the router doesn't exist
* @return string The generated URL
*/ */
protected function getRouteFromRouter($routerName, $routeId) { protected function getRouteFromRouter($routerName, $routeId, $parameters = array(), $referenceType = Router::ABSOLUTE_PATH) {
$route = $this->container->get($routerName)->getRouteCollection()->get($routeId); /** @var Router $router */
$router = $this->container->get($routerName);
if ($route == null) { if ($router == null) {
throw new \InvalidArgumentException(sprintf("Route ID '%s' does not exists.", $routeId)); throw new \InvalidArgumentException(sprintf("Router '%s' does not exists.", $routerName));
} }
return $route->getPath(); return $router->generate($routeId, $parameters, $referenceType);
} }
/** /**