From af22a785b59e19de38527ab582b60be1d1a00a2d Mon Sep 17 00:00:00 2001 From: Mohammed Date: Tue, 9 Jul 2024 10:38:12 +0100 Subject: [PATCH] Manage source url with site Access in UrlWildcardRouter --- .../bundle/Core/UrlWildcardRouter.php | 77 ++++++++++++++++++- 1 file changed, 73 insertions(+), 4 deletions(-) diff --git a/components/SEOBundle/bundle/Core/UrlWildcardRouter.php b/components/SEOBundle/bundle/Core/UrlWildcardRouter.php index cd1ab4668..d7a8ba1d7 100644 --- a/components/SEOBundle/bundle/Core/UrlWildcardRouter.php +++ b/components/SEOBundle/bundle/Core/UrlWildcardRouter.php @@ -14,27 +14,95 @@ use Exception; use Ibexa\Contracts\Core\Repository\URLWildcardService; +use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface; +use Ibexa\Core\MVC\Symfony\Routing\Generator\RouteReferenceGenerator; use Ibexa\Core\MVC\Symfony\Routing\UrlAliasRouter; use Ibexa\Core\MVC\Symfony\Routing\UrlWildcardRouter as BaseUrlWildcardRouter; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Exception\ResourceNotFoundException; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\Routing\RouterInterface; +use Ibexa\Core\MVC\Symfony\SiteAccess; class UrlWildcardRouter extends BaseUrlWildcardRouter { /** @var URLWildcardService */ private $wildcardService; + /** @var RouteReferenceGenerator */ + private $routeReferenceGenerator; + /** + * @var ConfigResolverInterface + */ + private $configResolver; + + /** + * @var RouterInterface + */ + private $router; + + /** + * @required + */ public function setWildcardService(URLWildcardService $wildcardService): void { $this->wildcardService = $wildcardService; } + /** + * @required + */ + public function setRouteReferenceGenerator(RouteReferenceGenerator $routeReferenceGenerator): void + { + $this->routeReferenceGenerator = $routeReferenceGenerator; + } + + /** + * @required + */ + public function setConfigResolverInterface(ConfigResolverInterface $configResolver): void + { + $this->configResolver = $configResolver; + } + + /** + * @required + */ + public function setRouter(RouterInterface $router): void + { + $this->router = $router; + } + public function matchRequest(Request $request): array { + // Manage full url : http://host.com/uri + $requestedPath = $request->attributes->get('semanticPathinfo', $request->getPathInfo()); try { - // Manage full url : http://host.com/uri - $requestedPath = $request->attributes->get('semanticPathinfo', $request->getPathInfo()); - $requestUriFull = $request->getSchemeAndHttpHost().$requestedPath; + // Manage full url with site Access : http://host.com/{siteAccess}/uri + + if ( + $request->attributes->has('siteaccess') && + $request->attributes->get('siteaccess') instanceof SiteAccess + ) { + /** @var SiteAccess $siteAccess */ + $siteAccess = $request->attributes->get('siteaccess'); + $locationId = $this->configResolver + ->getParameter('content.tree_root.location_id', null, $siteAccess->name); + $routeReference = $this->routeReferenceGenerator->generate( + UrlAliasRouter::URL_ALIAS_ROUTE_NAME, + [ + 'siteaccess' => $siteAccess->name, + 'locationId' => $locationId, + ] + ); + $requestUriFull = $this->router->generate( + $routeReference->getRoute(), + $routeReference->getParams(), + UrlGeneratorInterface::ABSOLUTE_URL + ).$requestedPath; + } else { + $requestUriFull = $request->getSchemeAndHttpHost().$requestedPath; + } $urlWildcard = $this->wildcardService->translate($requestUriFull); } catch (Exception $e) { try { @@ -48,9 +116,10 @@ public function matchRequest(Request $request): array $params = [ '_route' => UrlAliasRouter::URL_ALIAS_ROUTE_NAME, ]; - + //External URL if (0 === strpos($urlWildcard->uri, 'http://') || 'https://' === substr($urlWildcard->uri, 0, 8)) { $params += ['semanticPathinfo' => trim($urlWildcard->uri, '/')]; + $params += ['prependSiteaccessOnRedirect' => false]; } else { $params += ['semanticPathinfo' => '/'.trim($urlWildcard->uri, '/')]; }