@@ -297,62 +297,84 @@ public function tryBuildUrl($httpContext, $actionName, $controllerName = null, $
297297 }
298298
299299 $ segments = $ route ->getSegments ();
300-
301- $ last = null ;
300+ $ default = array ();
301+ $ path = '' ;
302302
303303 foreach ($ segments as $ segment ) {
304- $ result .= $ segment ->after ;
304+ $ isDefault = false ;
305+ $ path .= $ segment ->after ;
305306
306307 if (empty ($ segment ->name )) {
307- $ result .= $ segment ->pattern ;
308+ $ path .= $ segment ->pattern ;
308309 }
309310 elseif ($ segment ->name == 'controller ' && !empty ($ controllerName )) {
310- $ result .= $ controllerName ;
311+ $ path .= $ controllerName ;
312+ $ isDefault = (!empty ($ segment ->default ) && mb_strtolower ($ segment ->default ) === mb_strtolower ($ controllerName ));
311313 }
312314 elseif ($ segment ->name == 'controller ' && empty ($ controllerName ) && !empty ($ route ->values ['controller ' ])) {
313- $ result .= $ route ->values ['controller ' ];
315+ $ path .= $ route ->values ['controller ' ];
316+ $ isDefault = (!empty ($ segment ->default ) && mb_strtolower ($ segment ->default ) === mb_strtolower ($ route ->values ['controller ' ]));
314317 }
315318 elseif ($ segment ->name == 'action ' ) {
316- $ result .= $ actionName ;
319+ $ path .= $ actionName ;
320+ $ isDefault = (!empty ($ segment ->default ) && mb_strtolower ($ segment ->default ) === mb_strtolower ($ actionName ));
317321 }
318322 else {
319323 if (!empty ($ routeValues [$ segment ->name ])) {
320- $ result .= $ routeValues [$ segment ->name ];
324+ $ path .= $ routeValues [$ segment ->name ];
321325 unset($ routeValues [$ segment ->name ]);
326+ $ isDefault = false ;
322327 }
323328 else
324329 {
325330 if ($ segment ->default !== UrlParameter::OPTIONAL ) {
326- $ result .= $ segment ->default ;
331+ $ path .= $ segment ->default ;
332+ }
333+ else {
334+ $ isDefault = true ;
327335 }
328336 }
329337 }
330338
331- $ result .= $ segment ->before ;
339+ $ path .= $ segment ->before ;
332340
333- $ result .= '/ ' ;
341+ $ path .= '/ ' ;
334342
335- if ($ segment -> preEnd === true || ( $ segment -> end === true && $ segment -> default !== UrlParameter:: OPTIONAL ) ) {
336- $ last = $ segment ;
343+ if ($ isDefault ) {
344+ $ isDefault = empty ( $ segment-> before ) ;
337345 }
346+
347+ $ default [] = $ isDefault ;
338348 }
339349
340- $ result = rtrim ($ result , '/ ' );
350+ $ path = rtrim ($ path , '/ ' );
341351
342352 if ($ options ->removeLastSegmentIfValueIsDefault === true ) {
343- $ lastSegmentPos = strrpos ($ result , '/ ' );
344- $ lastSegment = trim (mb_substr ($ result , $ lastSegmentPos ), '/ ' );
353+ $ resultSegments = explode ('/ ' , $ path );
354+
355+ if (count ($ default ) > count ($ resultSegments )) {
356+ $ default = array_slice ($ default , 0 , count ($ resultSegments ));
357+ }
345358
346- if (empty ($ last ->before ) && !empty ($ last ->default ) && mb_strtolower ($ last ->default ) === mb_strtolower ($ lastSegment )) {
347- $ result = mb_substr ($ result , 0 , $ lastSegmentPos );
359+ for ($ i = count ($ resultSegments ) - 1 ; $ i >= 0 ; --$ i ) {
360+ if ($ default [$ i ]) {
361+ unset($ resultSegments [$ i ]);
362+ }
363+ else {
364+ break ;
365+ }
348366 }
367+
368+ $ path = implode ('/ ' , $ resultSegments );
349369 }
350370
371+ $ result .= $ path ;
372+
351373 if ($ options ->lowercaseUrls === true ) {
352374 $ result = mb_strtolower ($ result );
353375 }
354376
355- if ($ options ->appendTrailingSlash === true ) {
377+ if ($ options ->appendTrailingSlash === true && $ result !== ' / ' ) {
356378 $ result .= '/ ' ;
357379 }
358380
0 commit comments