@@ -38,7 +38,9 @@ abstract class Client
38
38
protected $ crawler ;
39
39
protected $ insulated = false ;
40
40
protected $ redirect ;
41
+ protected $ redirects = array ();
41
42
protected $ followRedirects = true ;
43
+ protected $ browserNavigation = false ;
42
44
43
45
private $ maxRedirects = -1 ;
44
46
private $ redirectCount = 0 ;
@@ -68,6 +70,16 @@ public function followRedirects($followRedirect = true)
68
70
$ this ->followRedirects = (bool ) $ followRedirect ;
69
71
}
70
72
73
+ /**
74
+ * Sets whether to emulate back/forward browser navigation (skip redirects).
75
+ *
76
+ * @param bool $browserNavigation Whether to emulate browser navigation
77
+ */
78
+ public function browserNavigation ($ browserNavigation = true )
79
+ {
80
+ $ this ->browserNavigation = (bool ) $ browserNavigation ;
81
+ }
82
+
71
83
/**
72
84
* Sets the maximum number of requests that crawler can follow.
73
85
*
@@ -308,6 +320,8 @@ public function request($method, $uri, array $parameters = array(), array $files
308
320
}
309
321
310
322
if ($ this ->followRedirects && $ this ->redirect ) {
323
+ $ this ->redirects [] = spl_object_hash ($ this ->history ->current ());
324
+
311
325
return $ this ->crawler = $ this ->followRedirect ();
312
326
}
313
327
@@ -410,7 +424,11 @@ protected function createCrawlerFromContent($uri, $content, $type)
410
424
*/
411
425
public function back ()
412
426
{
413
- return $ this ->requestFromRequest ($ this ->history ->back (), false );
427
+ do {
428
+ $ request = $ this ->history ->back ();
429
+ } while ($ this ->browserNavigation && in_array (spl_object_hash ($ request ), $ this ->redirects ));
430
+
431
+ return $ this ->requestFromRequest ($ request , false );
414
432
}
415
433
416
434
/**
@@ -420,7 +438,11 @@ public function back()
420
438
*/
421
439
public function forward ()
422
440
{
423
- return $ this ->requestFromRequest ($ this ->history ->forward (), false );
441
+ do {
442
+ $ request = $ this ->history ->forward ();
443
+ } while ($ this ->browserNavigation && in_array (spl_object_hash ($ request ), $ this ->redirects ));
444
+
445
+ return $ this ->requestFromRequest ($ request , false );
424
446
}
425
447
426
448
/**
0 commit comments