@@ -180,11 +180,12 @@ private function doMatch($url, Request $request = null)
180180 // matching requests is more powerful than matching URLs only, so try that first
181181 if ($ router instanceof RequestMatcherInterface) {
182182 if (empty ($ requestForMatching )) {
183- $ requestForMatching = Request:: create ($ url );
183+ $ requestForMatching = $ this -> rebuildRequest ($ url );
184184 }
185185
186186 return $ router ->matchRequest ($ requestForMatching );
187187 }
188+
188189 // every router implements the match method
189190 return $ router ->match ($ url );
190191 } catch (ResourceNotFoundException $ e ) {
@@ -249,6 +250,51 @@ public function generate($name, $parameters = array(), $absolute = UrlGeneratorI
249250 throw new RouteNotFoundException (sprintf ('None of the chained routers were able to generate route: %s ' , $ info ));
250251 }
251252
253+ /**
254+ * Rebuild the request object from a URL with the help of the RequestContext.
255+ *
256+ * If the request context is not set, this simply returns the request object built from $uri.
257+ *
258+ * @param string $uri
259+ *
260+ * @return Request
261+ */
262+ private function rebuildRequest ($ uri )
263+ {
264+ if (!$ this ->context ) {
265+ return Request::create ($ uri );
266+ }
267+
268+ $ server = array ();
269+ if ($ this ->context ->getHost ()) {
270+ $ server ['SERVER_NAME ' ] = $ this ->context ->getHost ();
271+ $ server ['HTTP_HOST ' ] = $ this ->context ->getHost ();
272+ }
273+ if ($ this ->context ->getBaseUrl ()) {
274+ $ uri = $ this ->context ->getBaseUrl ().$ uri ;
275+ $ server ['SCRIPT_FILENAME ' ] = $ this ->context ->getBaseUrl ();
276+ $ server ['PHP_SELF ' ] = $ this ->context ->getBaseUrl ();
277+ }
278+ if ('https ' === $ this ->context ->getScheme ()) {
279+ $ server ['HTTPS ' ] = 'on ' ;
280+ $ server ['SERVER_PORT ' ] = $ this ->context ->getHttpsPort ();
281+ if (443 !== $ this ->context ->getHttpsPort ()) {
282+ // this is parsed from the host by symfony request
283+ // https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L971
284+ $ server ['HTTP_HOST ' ] .= ': ' .$ this ->context ->getHttpsPort ();
285+ }
286+ } else {
287+ $ server ['SERVER_PORT ' ] = $ this ->context ->getHttpPort ();
288+ if (80 !== $ this ->context ->getHttpPort ()) {
289+ // this is parsed from the host by symfony request
290+ // https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/Request.php#L971
291+ $ server ['HTTP_HOST ' ] .= ': ' .$ this ->context ->getHttpPort ();
292+ }
293+ }
294+
295+ return Request::create ($ uri , $ this ->context ->getMethod (), $ this ->context ->getParameters (), array (), array (), $ server );
296+ }
297+
252298 private function getErrorMessage ($ name , $ router = null , $ parameters = null )
253299 {
254300 if ($ router instanceof VersatileGeneratorInterface) {
0 commit comments