@@ -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