diff --git a/Classes/Extbase/RouteHandler.php b/Classes/Extbase/RouteHandler.php index ee9c289..86081f3 100644 --- a/Classes/Extbase/RouteHandler.php +++ b/Classes/Extbase/RouteHandler.php @@ -67,6 +67,26 @@ public function __construct(RouteService $service, Bootstrap $bootstrap, Respons $this->bootstrap = $bootstrap; $this->routeService = $service; } + + /** + * Returns the Path of the requested URI without the site base path. + * + * @param ServerRequestInterface $request + * @return string + */ + private function slugFromRequest(ServerRequestInterface $request): string { + $slug = $request->getUri()->getPath(); + + // Remove the site base path from the slug - see: typo3/sysext/frontend/Classes/Middleware/StaticRouteResolver.php + if (($site = $request->getAttribute('site', null)) instanceof \TYPO3\CMS\Core\Site\Entity\Site) { + $siteBasePath = '/' . trim($site->getBase()->getPath(), '/'); + if(str_starts_with($slug, $siteBasePath)) { + $slug = substr($slug, strlen($siteBasePath)); + $slug = '/' . ltrim($slug, '/'); + } + } + return $slug; + } /** * @throws NoConfigurationException @@ -75,7 +95,7 @@ public function __construct(RouteService $service, Bootstrap $bootstrap, Respons */ public function handle(ServerRequestInterface $request) { - $slug = $request->getUri()->getPath(); + $slug = $this->slugFromRequest($request); try { $this->processRoute($request, $this->routeService->findRouteFor($slug)); @@ -130,7 +150,7 @@ private function processMiddleware(ServerRequestInterface $request): void return; } - $slug = $request->getUri()->getPath(); + $slug = $this->slugFromRequest($request); foreach ($this->routeService->findMiddlewareFor($slug) as $middlewareRoute) { $middleware = GeneralUtility::makeInstance(Middleware::class); diff --git a/Classes/Routing/RestApiEnhancer.php b/Classes/Routing/RestApiEnhancer.php index 8abeb5d..3f9feb4 100644 --- a/Classes/Routing/RestApiEnhancer.php +++ b/Classes/Routing/RestApiEnhancer.php @@ -44,7 +44,8 @@ public function enhanceForMatching(RouteCollection $collection): void { $defaultOptions = $collection->get('default')->getOptions(); - $apiRoute = new Route('/api/{any}', [], ['any' => '.*'], $defaultOptions); + $pathPrefix = empty($this->configuration['pathPrefix']) ? '/api/' : ('/' . trim($this->configuration['pathPrefix'], '/') . '/'); + $apiRoute = new Route($pathPrefix . '{any}', [], ['any' => '.*'], $defaultOptions); $collection->add('api', $apiRoute); }