Skip to content

Commit 8063b59

Browse files
committed
fixes
1 parent 2d02ee1 commit 8063b59

2 files changed

Lines changed: 38 additions & 9 deletions

File tree

src/Routing/RouteMap.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,28 @@ public function getName()
211211
/**
212212
* Set the name for this route and register it with the router for duplicate detection.
213213
*
214-
* @param mixed $name
214+
* @param string $name
215215
* @return RouteMap
216216
* @throws Exceptions\DuplicateRouteException If name is already in use
217217
*/
218-
public function setName( $name ) : RouteMap
218+
public function setName( string $name ) : RouteMap
219219
{
220-
// If router context is set, register the name for duplicate detection
220+
// If the name is already set to this value, no need to re-register
221+
if( $this->Name === $name )
222+
{
223+
return $this;
224+
}
225+
226+
// If router context is set, unregister old name and register the new one
221227
if( $this->_router && $this->_method )
222228
{
229+
// Unregister the old name if one exists
230+
if( $this->Name !== '' )
231+
{
232+
$this->_router->unregisterRouteName( $this->Name );
233+
}
234+
235+
// Register the new name (will check for duplicates)
223236
$this->_router->registerRouteName( $name, $this->_method, $this->Path, $this );
224237
}
225238

src/Routing/Router.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,18 @@ protected function addRoute( array &$routes, string $method, string $routeName,
235235
// Set router context so the route can register names for duplicate detection
236236
$route->setRouterContext( $this, $method );
237237

238-
// Set the route name if provided (this will trigger duplicate name check)
239-
if( $name )
238+
// Check for duplicate path+method combinations BEFORE setting the name
239+
// This ensures that if the path check throws, no name gets registered
240+
if( $this->_strictMode )
240241
{
241-
$route->setName( $name );
242+
$this->checkDuplicateRoute( $route, $method );
242243
}
243244

244-
// Check for duplicate path+method combinations if strict mode is enabled
245-
if( $this->_strictMode )
245+
// Set the route name if provided (this will trigger duplicate name check)
246+
// This is done AFTER path checking to avoid leaving stale names on error
247+
if( $name )
246248
{
247-
$this->checkDuplicateRoute( $route, $method );
249+
$route->setName( $name );
248250
}
249251

250252
$routes[] = $route;
@@ -297,6 +299,20 @@ public function registerRouteName( string $name, string $method, string $path, R
297299
$this->_registeredNames[ $name ] = "{$method}:{$path}";
298300
}
299301

302+
/**
303+
* Unregister a route name (called by RouteMap::setName when renaming).
304+
*
305+
* This method is called when a route's name is being changed, allowing
306+
* the old name to be removed from the registry before registering the new name.
307+
*
308+
* @param string $name The route name to unregister
309+
* @return void
310+
*/
311+
public function unregisterRouteName( string $name ): void
312+
{
313+
unset( $this->_registeredNames[ $name ] );
314+
}
315+
300316
/**
301317
* Check if a route is a duplicate and throw exception if found.
302318
*

0 commit comments

Comments
 (0)