@@ -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 ;
@@ -78,6 +80,16 @@ public function isFollowingRedirects()
78
80
return $ this ->followRedirects ;
79
81
}
80
82
83
+ /**
84
+ * Sets whether to emulate back/forward browser navigation (skip redirects).
85
+ *
86
+ * @param bool $browserNavigation Whether to emulate browser navigation
87
+ */
88
+ public function browserNavigation ($ browserNavigation = true )
89
+ {
90
+ $ this ->browserNavigation = (bool ) $ browserNavigation ;
91
+ }
92
+
81
93
/**
82
94
* Sets the maximum number of requests that crawler can follow.
83
95
*
@@ -328,6 +340,8 @@ public function request($method, $uri, array $parameters = array(), array $files
328
340
}
329
341
330
342
if ($ this ->followRedirects && $ this ->redirect ) {
343
+ $ this ->redirects [spl_object_hash ($ this ->history ->current ())] = true ;
344
+
331
345
return $ this ->crawler = $ this ->followRedirect ();
332
346
}
333
347
@@ -430,7 +444,11 @@ protected function createCrawlerFromContent($uri, $content, $type)
430
444
*/
431
445
public function back ()
432
446
{
433
- return $ this ->requestFromRequest ($ this ->history ->back (), false );
447
+ do {
448
+ $ request = $ this ->history ->back ();
449
+ } while ($ this ->browserNavigation && array_key_exists (spl_object_hash ($ request ), $ this ->redirects ));
450
+
451
+ return $ this ->requestFromRequest ($ request , false );
434
452
}
435
453
436
454
/**
@@ -440,7 +458,11 @@ public function back()
440
458
*/
441
459
public function forward ()
442
460
{
443
- return $ this ->requestFromRequest ($ this ->history ->forward (), false );
461
+ do {
462
+ $ request = $ this ->history ->forward ();
463
+ } while ($ this ->browserNavigation && array_key_exists (spl_object_hash ($ request ), $ this ->redirects ));
464
+
465
+ return $ this ->requestFromRequest ($ request , false );
444
466
}
445
467
446
468
/**
0 commit comments