@@ -230,15 +230,18 @@ protected function addRoute( array &$routes, string $method, string $routeName,
230230 $ routeName = substr ( $ routeName , 0 , -1 );
231231 }
232232
233- $ route = new RouteMap ( $ routeName , $ function , $ filters ?? '' );
233+ $ route = new RouteMap ( $ routeName , $ function , $ filters ?? '' );
234234
235- // Set the route name BEFORE checking for duplicates
235+ // Set router context so the route can register names for duplicate detection
236+ $ route ->setRouterContext ( $ this , $ method );
237+
238+ // Set the route name if provided (this will trigger duplicate name check)
236239 if ( $ name )
237240 {
238241 $ route ->setName ( $ name );
239242 }
240243
241- // Check for duplicate routes if strict mode is enabled
244+ // Check for duplicate path+method combinations if strict mode is enabled
242245 if ( $ this ->_strictMode )
243246 {
244247 $ this ->checkDuplicateRoute ( $ route , $ method );
@@ -249,6 +252,51 @@ protected function addRoute( array &$routes, string $method, string $routeName,
249252 return $ route ;
250253 }
251254
255+ /**
256+ * Register a route name and check for duplicates (called by RouteMap::setName).
257+ *
258+ * This method is called when a route name is set via the fluent API,
259+ * allowing duplicate name detection to work properly even when names
260+ * are set after route registration.
261+ *
262+ * @param string $name The route name to register
263+ * @param string $method The HTTP method (GET, POST, PUT, DELETE)
264+ * @param string $path The route path
265+ * @param RouteMap $route The route being named
266+ * @return void
267+ * @throws Exceptions\DuplicateRouteException If name is already in use
268+ */
269+ public function registerRouteName ( string $ name , string $ method , string $ path , RouteMap $ route ): void
270+ {
271+ // Only check for duplicates if strict mode is enabled
272+ if ( !$ this ->_strictMode )
273+ {
274+ $ this ->_registeredNames [ $ name ] = "{$ method }: {$ path }" ;
275+ return ;
276+ }
277+
278+ // Check if this name is already registered
279+ if ( isset ( $ this ->_registeredNames [ $ name ] ) )
280+ {
281+ // Parse the original route's signature to get method and path
282+ $ originalSignature = $ this ->_registeredNames [ $ name ];
283+ list ( $ originalMethod , $ originalPath ) = explode ( ': ' , $ originalSignature , 2 );
284+
285+ throw new Exceptions \DuplicateRouteException (
286+ $ originalMethod , // First route method
287+ $ originalPath , // First route path
288+ $ this ->_registeredRoutes [ $ originalSignature ] ?? 'unknown controller ' ,
289+ $ method , // Second route method
290+ $ path , // Second route path
291+ $ this ->extractControllerInfo ( $ route ),
292+ $ name
293+ );
294+ }
295+
296+ // Register the name
297+ $ this ->_registeredNames [ $ name ] = "{$ method }: {$ path }" ;
298+ }
299+
252300 /**
253301 * Check if a route is a duplicate and throw exception if found.
254302 *
@@ -277,32 +325,8 @@ protected function checkDuplicateRoute( RouteMap $route, string $method ): void
277325 );
278326 }
279327
280- // Check for duplicate route name
281- $ name = $ route ->getName ();
282- if ( $ name && isset ( $ this ->_registeredNames [ $ name ] ) )
283- {
284- // Parse the original route's signature to get method and path
285- $ originalSignature = $ this ->_registeredNames [ $ name ];
286- list ( $ originalMethod , $ originalPath ) = explode ( ': ' , $ originalSignature , 2 );
287-
288- throw new Exceptions \DuplicateRouteException (
289- $ originalMethod , // First route method
290- $ originalPath , // First route path
291- $ this ->_registeredRoutes [ $ originalSignature ],
292- $ method , // Second route method
293- $ path , // Second route path
294- $ this ->extractControllerInfo ( $ route ),
295- $ name
296- );
297- }
298-
299- // Register this route
328+ // Register this route (name registration is handled separately by registerRouteName)
300329 $ this ->_registeredRoutes [ $ signature ] = $ this ->extractControllerInfo ( $ route );
301-
302- if ( $ name )
303- {
304- $ this ->_registeredNames [ $ name ] = $ signature ;
305- }
306330 }
307331
308332 /**
0 commit comments