@@ -86,7 +86,7 @@ public static function addRoute(Route $route): void
8686 }
8787
8888 foreach ($ params as $ key => $ index ) {
89- $ route ->setPathParam ($ key , $ index );
89+ $ route ->setPathParam ($ key , $ index, $ path );
9090 }
9191
9292 self ::$ routes [$ route ->getMethod ()][$ path ] = $ route ;
@@ -101,12 +101,16 @@ public static function addRoute(Route $route): void
101101 */
102102 public static function addRouteAlias (string $ path , Route $ route ): void
103103 {
104- [$ alias ] = self ::preparePath ($ path );
104+ [$ alias, $ params ] = self ::preparePath ($ path );
105105
106106 if (array_key_exists ($ alias , self ::$ routes [$ route ->getMethod ()]) && !self ::$ allowOverride ) {
107107 throw new Exception ("Route for ( {$ route ->getMethod ()}: {$ alias }) already registered. " );
108108 }
109109
110+ foreach ($ params as $ key => $ index ) {
111+ $ route ->setPathParam ($ key , $ index , $ alias );
112+ }
113+
110114 self ::$ routes [$ route ->getMethod ()][$ alias ] = $ route ;
111115 }
112116
@@ -123,7 +127,7 @@ public static function match(string $method, string $path): Route|null
123127 return null ;
124128 }
125129
126- $ parts = array_values (array_filter (explode ('/ ' , $ path )));
130+ $ parts = array_values (array_filter (explode ('/ ' , $ path ), fn ( $ segment ) => $ segment !== '' ));
127131 $ length = count ($ parts ) - 1 ;
128132 $ filteredParams = array_filter (self ::$ params , fn ($ i ) => $ i <= $ length );
129133
@@ -138,7 +142,9 @@ public static function match(string $method, string $path): Route|null
138142 );
139143
140144 if (array_key_exists ($ match , self ::$ routes [$ method ])) {
141- return self ::$ routes [$ method ][$ match ];
145+ $ route = self ::$ routes [$ method ][$ match ];
146+ $ route ->setMatchedPath ($ match );
147+ return $ route ;
142148 }
143149 }
144150
@@ -147,7 +153,9 @@ public static function match(string $method, string $path): Route|null
147153 */
148154 $ match = self ::WILDCARD_TOKEN ;
149155 if (array_key_exists ($ match , self ::$ routes [$ method ])) {
150- return self ::$ routes [$ method ][$ match ];
156+ $ route = self ::$ routes [$ method ][$ match ];
157+ $ route ->setMatchedPath ($ match );
158+ return $ route ;
151159 }
152160
153161 /**
@@ -157,7 +165,9 @@ public static function match(string $method, string $path): Route|null
157165 $ current = ($ current ?? '' ) . "{$ part }/ " ;
158166 $ match = $ current . self ::WILDCARD_TOKEN ;
159167 if (array_key_exists ($ match , self ::$ routes [$ method ])) {
160- return self ::$ routes [$ method ][$ match ];
168+ $ route = self ::$ routes [$ method ][$ match ];
169+ $ route ->setMatchedPath ($ match );
170+ return $ route ;
161171 }
162172 }
163173
@@ -192,7 +202,7 @@ protected static function combinations(array $set): iterable
192202 * @param string $path
193203 * @return array
194204 */
195- protected static function preparePath (string $ path ): array
205+ public static function preparePath (string $ path ): array
196206 {
197207 $ parts = array_values (array_filter (explode ('/ ' , $ path )));
198208 $ prepare = '' ;
0 commit comments