88use Illuminate \Routing \Router ;
99use Illuminate \Routing \UrlGenerator ;
1010use Illuminate \Support \Collection ;
11+ use Illuminate \Support \Facades \URL ;
1112use Illuminate \Support \Str ;
1213use Illuminate \View \Factory ;
1314use ReflectionProperty ;
@@ -54,7 +55,9 @@ public function handle()
5455 $ this ->forcedScheme = (new ReflectionProperty ($ this ->url , 'forceScheme ' ))->getValue ($ this ->url );
5556 $ this ->forcedRoot = (new ReflectionProperty ($ this ->url , 'forcedRoot ' ))->getValue ($ this ->url );
5657
57- $ routes = collect ($ this ->router ->getRoutes ())->map (function (BaseRoute $ route ) {
58+ $ globalUrlDefaults = collect (URL ::getDefaultParameters ())->map (fn ($ v ) => is_scalar ($ v ) || is_null ($ v ) ? $ v : '' );
59+
60+ $ routes = collect ($ this ->router ->getRoutes ())->map (function (BaseRoute $ route ) use ($ globalUrlDefaults ) {
5861 $ defaults = collect ($ this ->router ->gatherRouteMiddleware ($ route ))->map (function ($ middleware ) {
5962 if ($ middleware instanceof \Closure) {
6063 return [];
@@ -65,7 +68,7 @@ public function handle()
6568 return $ this ->urlDefaults [$ middleware ];
6669 })->flatMap (fn ($ r ) => $ r );
6770
68- return new Route ($ route , $ defaults , $ this ->forcedScheme , $ this ->forcedRoot );
71+ return new Route ($ route , $ globalUrlDefaults -> merge ( $ defaults) , $ this ->forcedScheme , $ this ->forcedRoot );
6972 });
7073
7174 if (! $ this ->option ('skip-actions ' )) {
@@ -228,6 +231,10 @@ private function appendCommonImports(Collection $routes, string $path, string $n
228231 $ imports [] = 'type RouteFormDefinition ' ;
229232 }
230233
234+ if ($ routes ->contains (fn (Route $ route ) => $ route ->parameters ()->isNotEmpty ())) {
235+ $ imports [] = 'applyUrlDefaults ' ;
236+ }
237+
231238 if ($ routes ->contains (fn (Route $ route ) => $ route ->parameters ()->contains (fn (Parameter $ parameter ) => $ parameter ->optional ))) {
232239 $ imports [] = 'validateParameters ' ;
233240 }
@@ -340,6 +347,12 @@ private function getDefaultsForMiddleware(string $middleware)
340347 }
341348
342349 $ methodContents = str ($ methodContents )->after ('{ ' )->beforeLast ('} ' )->trim ();
350+
351+ return $ this ->extractUrlDefaults ($ methodContents );
352+ }
353+
354+ private function extractUrlDefaults (string $ methodContents ): array
355+ {
343356 $ tokens = token_get_all ('<?php ' .$ methodContents );
344357 $ foundUrlFacade = false ;
345358 $ defaults = [];
0 commit comments