Skip to content

Commit 2a3e57c

Browse files
TDannhauerralflang
authored andcommitted
Refactor route prefixing logic in RuntimeRoutesMapper
Refactor route handling to ensure secondary paths are prefixed correctly with the current app webroot. Update addRoute and addSecondary methods for better clarity and functionality.
1 parent d27c479 commit 2a3e57c

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

src/RuntimeRoutesMapper.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,30 +131,29 @@ public function connect($first, $second = null, $third = null)
131131
/**
132132
* Prefix secondary route paths with the current app webroot.
133133
*
134-
* buildRoute() already prefixes the primary URI; secondary paths added via
135-
* withSecondaryRoute() are app-relative and need the same prefix for
136-
* routematch(). We build the RouteBuilder here, prefix secondary Route
137-
* objects, and pass the final array to the parent.
134+
* buildRoute() already prefixes the primary URI; withSecondaryRoute() paths
135+
* are app-relative. Prefix them on the builder before build() so Route's
136+
* match regex (built in the constructor) uses the full path.
138137
*/
139138
public function addRoute(RouteBuilder|Route|array $routeOrBuilder): void
140139
{
141-
if ($this->currentAppPrefix === '') {
142-
parent::addRoute($routeOrBuilder);
143-
return;
140+
if ($routeOrBuilder instanceof RouteBuilder && $this->currentAppPrefix !== '') {
141+
$routeOrBuilder->prefixSecondaryPaths($this->currentAppPrefix);
144142
}
145143

146-
if ($routeOrBuilder instanceof RouteBuilder) {
147-
$routeOrBuilder = $routeOrBuilder->build();
148-
}
144+
parent::addRoute($routeOrBuilder);
145+
}
149146

150-
$routes = is_array($routeOrBuilder) ? $routeOrBuilder : [$routeOrBuilder];
151-
foreach ($routes as $route) {
152-
if ($route->secondary && !str_starts_with($route->routePath, $this->currentAppPrefix)) {
153-
$route->routePath = $this->currentAppPrefix . '/' . ltrim($route->routePath, '/');
154-
}
147+
/**
148+
* Prefix legacy addSecondary() paths with the current app webroot.
149+
*/
150+
public function addSecondary(string $path, string $namedRoute): void
151+
{
152+
if ($this->currentAppPrefix !== '') {
153+
$path = $this->currentAppPrefix . '/' . ltrim($path, '/');
155154
}
156155

157-
parent::addRoute($routes);
156+
parent::addSecondary($path, $namedRoute);
158157
}
159158

160159
/**

0 commit comments

Comments
 (0)